use :: libc;
extern "C" {
    pub type event_base;
    pub type evbuffer;
    pub type bufferevent_ops;
    pub type args;
    pub type tmuxpeer;
    pub type hyperlinks;
    pub type screen_sel;
    pub type screen_titles;
    pub type environ;
    pub type options;
    pub type format_tree;
    pub type input_ctx;
    pub type cmds;
    pub type tty_key;
    pub type tty_code;
    pub type format_job_tree;
    pub type control_state;
    pub type cmdq_list;
    fn free(_: *mut libc::c_void);
    fn memcpy(_: *mut libc::c_void, _: *const libc::c_void, _: u64) -> *mut libc::c_void;
    fn memset(_: *mut libc::c_void, _: i32, _: u64) -> *mut libc::c_void;
    fn strlen(_: *const i8) -> u64;
    fn event_add(ev: *mut event, timeout: *const timeval) -> i32;
    fn event_pending(ev: *const event, events: i16, tv: *mut timeval) -> i32;
    fn event_initialized(ev: *const event) -> i32;
    fn event_set(
        _: *mut event,
        _: i32,
        _: i16,
        _: Option<unsafe extern "C" fn(i32, i16, *mut libc::c_void) -> ()>,
        _: *mut libc::c_void,
    );
    fn xmalloc(_: size_t) -> *mut libc::c_void;
    fn xcalloc(_: size_t, _: size_t) -> *mut libc::c_void;
    fn xvasprintf(_: *mut *mut i8, _: *const i8, _: ::core::ffi::VaList) -> i32;
    static mut global_options: *mut options;
    fn format_draw(
        _: *mut screen_write_ctx,
        _: *const grid_cell,
        _: u_int,
        _: *const i8,
        _: *mut style_ranges,
        _: i32,
    );
    fn options_get_number(_: *mut options, _: *const i8) -> i64;
    fn tty_window_offset(
        _: *mut tty,
        _: *mut u_int,
        _: *mut u_int,
        _: *mut u_int,
        _: *mut u_int,
    ) -> i32;
    fn tty_update_window_offset(_: *mut window);
    fn tty_write(_: Option<unsafe extern "C" fn(*mut tty, *const tty_ctx) -> ()>, _: *mut tty_ctx);
    fn tty_cmd_alignmenttest(_: *mut tty, _: *const tty_ctx);
    fn tty_cmd_cell(_: *mut tty, _: *const tty_ctx);
    fn tty_cmd_cells(_: *mut tty, _: *const tty_ctx);
    fn tty_cmd_clearendofscreen(_: *mut tty, _: *const tty_ctx);
    fn tty_cmd_clearscreen(_: *mut tty, _: *const tty_ctx);
    fn tty_cmd_clearstartofscreen(_: *mut tty, _: *const tty_ctx);
    fn tty_cmd_deletecharacter(_: *mut tty, _: *const tty_ctx);
    fn tty_cmd_clearcharacter(_: *mut tty, _: *const tty_ctx);
    fn tty_cmd_deleteline(_: *mut tty, _: *const tty_ctx);
    fn tty_cmd_insertcharacter(_: *mut tty, _: *const tty_ctx);
    fn tty_cmd_insertline(_: *mut tty, _: *const tty_ctx);
    fn tty_cmd_scrollup(_: *mut tty, _: *const tty_ctx);
    fn tty_cmd_scrolldown(_: *mut tty, _: *const tty_ctx);
    fn tty_cmd_reverseindex(_: *mut tty, _: *const tty_ctx);
    fn tty_cmd_setselection(_: *mut tty, _: *const tty_ctx);
    fn tty_cmd_rawstring(_: *mut tty, _: *const tty_ctx);
    fn tty_cmd_syncstart(_: *mut tty, _: *const tty_ctx);
    fn tty_default_colours(_: *mut grid_cell, _: *mut window_pane);
    fn tty_acs_double_borders(_: i32) -> *const utf8_data;
    fn tty_acs_heavy_borders(_: i32) -> *const utf8_data;
    fn tty_acs_rounded_borders(_: i32) -> *const utf8_data;
    fn status_at_line(_: *mut client) -> i32;
    fn status_line_size(_: *mut client) -> u_int;
    static grid_default_cell: grid_cell;
    fn grid_cells_equal(_: *const grid_cell, _: *const grid_cell) -> i32;
    fn grid_clear_history(_: *mut grid);
    fn grid_get_cell(_: *mut grid, _: u_int, _: u_int, _: *mut grid_cell);
    fn grid_get_line(_: *mut grid, _: u_int) -> *mut grid_line;
    fn screen_reset_tabs(_: *mut screen);
    fn screen_select_cell(_: *mut screen, _: *mut grid_cell, _: *const grid_cell);
    fn screen_check_selection(_: *mut screen, _: u_int, _: u_int) -> i32;
    fn grid_view_delete_cells(_: *mut grid, _: u_int, _: u_int, _: u_int, _: u_int);
    fn grid_view_insert_cells(_: *mut grid, _: u_int, _: u_int, _: u_int, _: u_int);
    fn grid_view_delete_lines_region(_: *mut grid, _: u_int, _: u_int, _: u_int, _: u_int);
    fn grid_view_delete_lines(_: *mut grid, _: u_int, _: u_int, _: u_int);
    fn grid_view_insert_lines_region(_: *mut grid, _: u_int, _: u_int, _: u_int, _: u_int);
    fn grid_view_insert_lines(_: *mut grid, _: u_int, _: u_int, _: u_int);
    fn grid_view_scroll_region_down(_: *mut grid, _: u_int, _: u_int, _: u_int);
    fn grid_view_scroll_region_up(_: *mut grid, _: u_int, _: u_int, _: u_int);
    fn grid_view_clear(_: *mut grid, _: u_int, _: u_int, _: u_int, _: u_int, _: u_int);
    fn grid_view_clear_history(_: *mut grid, _: u_int);
    fn grid_view_set_cells(
        _: *mut grid,
        _: u_int,
        _: u_int,
        _: *const grid_cell,
        _: *const i8,
        _: size_t,
    );
    fn grid_view_set_padding(_: *mut grid, _: u_int, _: u_int);
    fn grid_view_set_cell(_: *mut grid, _: u_int, _: u_int, _: *const grid_cell);
    fn grid_view_get_cell(_: *mut grid, _: u_int, _: u_int, _: *mut grid_cell);
    fn screen_mode_to_string(_: i32) -> *const i8;
    fn layout_fix_panes(_: *mut window, _: *mut window_pane);
    fn screen_alternate_on(_: *mut screen, _: *mut grid_cell, _: i32);
    fn screen_alternate_off(_: *mut screen, _: *mut grid_cell, _: i32);
    fn session_has(_: *mut session, _: *mut window) -> i32;
    fn utf8_is_vs(_: *const utf8_data) -> i32;
    fn utf8_append(_: *mut utf8_data, _: u_char) -> utf8_state;
    fn utf8_open(_: *mut utf8_data, _: u_char) -> utf8_state;
    fn utf8_has_zwj(_: *const utf8_data) -> i32;
    fn utf8_is_zwj(_: *const utf8_data) -> i32;
    fn utf8_copy(_: *mut utf8_data, _: *const utf8_data);
    fn utf8_fromcstr(_: *const i8) -> *mut utf8_data;
    fn utf8_set(_: *mut utf8_data, _: u_char);
    fn fatalx(_: *const i8, _: ...) -> !;
    fn log_get_level() -> i32;
    fn log_debug(_: *const i8, _: ...);
    fn utf8_is_modifier(_: *const utf8_data) -> i32;
}
pub type __builtin_va_list = [__va_list_tag; 1];
#[derive(Copy, Clone)]
#[repr(C)]
pub struct __va_list_tag {
    pub gp_offset: u32,
    pub fp_offset: u32,
    pub overflow_arg_area: *mut libc::c_void,
    pub reg_save_area: *mut libc::c_void,
}
pub type __u_char = u8;
pub type __u_short = u16;
pub type __u_int = u32;
pub type __uint8_t = u8;
pub type __uint64_t = u64;
pub type __pid_t = i32;
pub type __time_t = i64;
pub type __suseconds_t = i64;
pub type __ssize_t = i64;
pub type u_char = __u_char;
pub type u_short = __u_short;
pub type u_int = __u_int;
pub type pid_t = __pid_t;
pub type ssize_t = __ssize_t;
pub type time_t = __time_t;
pub type size_t = u64;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct timeval {
    pub tv_sec: __time_t,
    pub tv_usec: __suseconds_t,
}
pub type va_list = __builtin_va_list;
pub type cc_t = u8;
pub type speed_t = u32;
pub type tcflag_t = u32;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct termios {
    pub c_iflag: tcflag_t,
    pub c_oflag: tcflag_t,
    pub c_cflag: tcflag_t,
    pub c_lflag: tcflag_t,
    pub c_line: cc_t,
    pub c_cc: [cc_t; 32],
    pub c_ispeed: speed_t,
    pub c_ospeed: speed_t,
}
pub type uint8_t = __uint8_t;
pub type uint64_t = __uint64_t;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct event {
    pub ev_evcallback: event_callback,
    pub ev_timeout_pos: C2RustUnnamed_4,
    pub ev_fd: i32,
    pub ev_base: *mut event_base,
    pub ev_: C2RustUnnamed,
    pub ev_events: i16,
    pub ev_res: i16,
    pub ev_timeout: timeval,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub union C2RustUnnamed {
    pub ev_io: C2RustUnnamed_2,
    pub ev_signal: C2RustUnnamed_0,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_0 {
    pub ev_signal_next: C2RustUnnamed_1,
    pub ev_ncalls: i16,
    pub ev_pncalls: *mut i16,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_1 {
    pub le_next: *mut event,
    pub le_prev: *mut *mut event,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_2 {
    pub ev_io_next: C2RustUnnamed_3,
    pub ev_timeout: timeval,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_3 {
    pub le_next: *mut event,
    pub le_prev: *mut *mut event,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub union C2RustUnnamed_4 {
    pub ev_next_with_common_timeout: C2RustUnnamed_5,
    pub min_heap_idx: i32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_5 {
    pub tqe_next: *mut event,
    pub tqe_prev: *mut *mut event,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct event_callback {
    pub evcb_active_next: C2RustUnnamed_7,
    pub evcb_flags: i16,
    pub evcb_pri: uint8_t,
    pub evcb_closure: uint8_t,
    pub evcb_cb_union: C2RustUnnamed_6,
    pub evcb_arg: *mut libc::c_void,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub union C2RustUnnamed_6 {
    pub evcb_callback: Option<unsafe extern "C" fn(i32, i16, *mut libc::c_void) -> ()>,
    pub evcb_selfcb: Option<unsafe extern "C" fn(*mut event_callback, *mut libc::c_void) -> ()>,
    pub evcb_evfinalize: Option<unsafe extern "C" fn(*mut event, *mut libc::c_void) -> ()>,
    pub evcb_cbfinalize: Option<unsafe extern "C" fn(*mut event_callback, *mut libc::c_void) -> ()>,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_7 {
    pub tqe_next: *mut event_callback,
    pub tqe_prev: *mut *mut event_callback,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct bufferevent {
    pub ev_base: *mut event_base,
    pub be_ops: *const bufferevent_ops,
    pub ev_read: event,
    pub ev_write: event,
    pub input: *mut evbuffer,
    pub output: *mut evbuffer,
    pub wm_read: event_watermark,
    pub wm_write: event_watermark,
    pub readcb: bufferevent_data_cb,
    pub writecb: bufferevent_data_cb,
    pub errorcb: bufferevent_event_cb,
    pub cbarg: *mut libc::c_void,
    pub timeout_read: timeval,
    pub timeout_write: timeval,
    pub enabled: i16,
}
pub type bufferevent_event_cb =
    Option<unsafe extern "C" fn(*mut bufferevent, i16, *mut libc::c_void) -> ()>;
pub type bufferevent_data_cb =
    Option<unsafe extern "C" fn(*mut bufferevent, *mut libc::c_void) -> ()>;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct event_watermark {
    pub low: size_t,
    pub high: size_t,
}
pub type bitstr_t = u8;
pub type msgtype = u32;
pub const MSG_READ_CANCEL: msgtype = 307;
pub const MSG_WRITE_CLOSE: msgtype = 306;
pub const MSG_WRITE_READY: msgtype = 305;
pub const MSG_WRITE: msgtype = 304;
pub const MSG_WRITE_OPEN: msgtype = 303;
pub const MSG_READ_DONE: msgtype = 302;
pub const MSG_READ: msgtype = 301;
pub const MSG_READ_OPEN: msgtype = 300;
pub const MSG_FLAGS: msgtype = 218;
pub const MSG_EXEC: msgtype = 217;
pub const MSG_WAKEUP: msgtype = 216;
pub const MSG_UNLOCK: msgtype = 215;
pub const MSG_SUSPEND: msgtype = 214;
pub const MSG_OLDSTDOUT: msgtype = 213;
pub const MSG_OLDSTDIN: msgtype = 212;
pub const MSG_OLDSTDERR: msgtype = 211;
pub const MSG_SHUTDOWN: msgtype = 210;
pub const MSG_SHELL: msgtype = 209;
pub const MSG_RESIZE: msgtype = 208;
pub const MSG_READY: msgtype = 207;
pub const MSG_LOCK: msgtype = 206;
pub const MSG_EXITING: msgtype = 205;
pub const MSG_EXITED: msgtype = 204;
pub const MSG_EXIT: msgtype = 203;
pub const MSG_DETACHKILL: msgtype = 202;
pub const MSG_DETACH: msgtype = 201;
pub const MSG_COMMAND: msgtype = 200;
pub const MSG_IDENTIFY_TERMINFO: msgtype = 112;
pub const MSG_IDENTIFY_LONGFLAGS: msgtype = 111;
pub const MSG_IDENTIFY_STDOUT: msgtype = 110;
pub const MSG_IDENTIFY_FEATURES: msgtype = 109;
pub const MSG_IDENTIFY_CWD: msgtype = 108;
pub const MSG_IDENTIFY_CLIENTPID: msgtype = 107;
pub const MSG_IDENTIFY_DONE: msgtype = 106;
pub const MSG_IDENTIFY_ENVIRON: msgtype = 105;
pub const MSG_IDENTIFY_STDIN: msgtype = 104;
pub const MSG_IDENTIFY_OLDCWD: msgtype = 103;
pub const MSG_IDENTIFY_TTYNAME: msgtype = 102;
pub const MSG_IDENTIFY_TERM: msgtype = 101;
pub const MSG_IDENTIFY_FLAGS: msgtype = 100;
pub const MSG_VERSION: msgtype = 12;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct client {
    pub name: *const i8,
    pub peer: *mut tmuxpeer,
    pub queue: *mut cmdq_list,
    pub windows: client_windows,
    pub control_state: *mut control_state,
    pub pause_age: u_int,
    pub pid: pid_t,
    pub fd: i32,
    pub out_fd: i32,
    pub event: event,
    pub retval: i32,
    pub creation_time: timeval,
    pub activity_time: timeval,
    pub last_activity_time: timeval,
    pub environ: *mut environ,
    pub jobs: *mut format_job_tree,
    pub title: *mut i8,
    pub path: *mut i8,
    pub cwd: *const i8,
    pub term_name: *mut i8,
    pub term_features: i32,
    pub term_type: *mut i8,
    pub term_caps: *mut *mut i8,
    pub term_ncaps: u_int,
    pub ttyname: *mut i8,
    pub tty: tty,
    pub written: size_t,
    pub discarded: size_t,
    pub redraw: size_t,
    pub repeat_timer: event,
    pub click_timer: event,
    pub click_button: u_int,
    pub click_event: mouse_event,
    pub status: status_line,
    pub flags: uint64_t,
    pub exit_type: C2RustUnnamed_33,
    pub exit_msgtype: msgtype,
    pub exit_session: *mut i8,
    pub exit_message: *mut i8,
    pub keytable: *mut key_table,
    pub last_key: key_code,
    pub redraw_panes: uint64_t,
    pub redraw_scrollbars: uint64_t,
    pub message_ignore_keys: i32,
    pub message_ignore_styles: i32,
    pub message_string: *mut i8,
    pub message_timer: event,
    pub prompt_string: *mut i8,
    pub prompt_buffer: *mut utf8_data,
    pub prompt_last: *mut i8,
    pub prompt_index: size_t,
    pub prompt_inputcb: prompt_input_cb,
    pub prompt_freecb: prompt_free_cb,
    pub prompt_data: *mut libc::c_void,
    pub prompt_hindex: [u_int; 4],
    pub prompt_mode: C2RustUnnamed_30,
    pub prompt_saved: *mut utf8_data,
    pub prompt_flags: i32,
    pub prompt_type: prompt_type,
    pub prompt_cursor: i32,
    pub session: *mut session,
    pub last_session: *mut session,
    pub references: i32,
    pub pan_window: *mut libc::c_void,
    pub pan_ox: u_int,
    pub pan_oy: u_int,
    pub overlay_check: overlay_check_cb,
    pub overlay_mode: overlay_mode_cb,
    pub overlay_draw: overlay_draw_cb,
    pub overlay_key: overlay_key_cb,
    pub overlay_free: overlay_free_cb,
    pub overlay_resize: overlay_resize_cb,
    pub overlay_data: *mut libc::c_void,
    pub overlay_timer: event,
    pub files: client_files,
    pub clipboard_panes: *mut u_int,
    pub clipboard_npanes: u_int,
    pub entry: C2RustUnnamed_8,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_8 {
    pub tqe_next: *mut client,
    pub tqe_prev: *mut *mut client,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct client_files {
    pub rbh_root: *mut client_file,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct client_file {
    pub c: *mut client,
    pub peer: *mut tmuxpeer,
    pub tree: *mut client_files,
    pub references: i32,
    pub stream: i32,
    pub path: *mut i8,
    pub buffer: *mut evbuffer,
    pub event: *mut bufferevent,
    pub fd: i32,
    pub error: i32,
    pub closed: i32,
    pub cb: client_file_cb,
    pub data: *mut libc::c_void,
    pub entry: C2RustUnnamed_9,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_9 {
    pub rbe_left: *mut client_file,
    pub rbe_right: *mut client_file,
    pub rbe_parent: *mut client_file,
    pub rbe_color: i32,
}
pub type client_file_cb = Option<
    unsafe extern "C" fn(*mut client, *const i8, i32, i32, *mut evbuffer, *mut libc::c_void) -> (),
>;
pub type overlay_resize_cb = Option<unsafe extern "C" fn(*mut client, *mut libc::c_void) -> ()>;
pub type overlay_free_cb = Option<unsafe extern "C" fn(*mut client, *mut libc::c_void) -> ()>;
pub type overlay_key_cb =
    Option<unsafe extern "C" fn(*mut client, *mut libc::c_void, *mut key_event) -> i32>;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct key_event {
    pub key: key_code,
    pub m: mouse_event,
    pub buf: *mut i8,
    pub len: size_t,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct mouse_event {
    pub valid: i32,
    pub ignore: i32,
    pub key: key_code,
    pub statusat: i32,
    pub statuslines: u_int,
    pub x: u_int,
    pub y: u_int,
    pub b: u_int,
    pub lx: u_int,
    pub ly: u_int,
    pub lb: u_int,
    pub ox: u_int,
    pub oy: u_int,
    pub s: i32,
    pub w: i32,
    pub wp: i32,
    pub sgr_type: u_int,
    pub sgr_b: u_int,
}
pub type key_code = u64;
pub type overlay_draw_cb =
    Option<unsafe extern "C" fn(*mut client, *mut libc::c_void, *mut screen_redraw_ctx) -> ()>;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct screen_redraw_ctx {
    pub c: *mut client,
    pub statuslines: u_int,
    pub statustop: i32,
    pub pane_status: i32,
    pub pane_lines: pane_lines,
    pub pane_scrollbars: i32,
    pub pane_scrollbars_pos: i32,
    pub no_pane_gc: grid_cell,
    pub no_pane_gc_set: i32,
    pub sx: u_int,
    pub sy: u_int,
    pub ox: u_int,
    pub oy: u_int,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct grid_cell {
    pub data: utf8_data,
    pub attr: u_short,
    pub flags: u_char,
    pub fg: i32,
    pub bg: i32,
    pub us: i32,
    pub link: u_int,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct utf8_data {
    pub data: [u_char; 21],
    pub have: u_char,
    pub size: u_char,
    pub width: u_char,
}
pub type pane_lines = u32;
pub const PANE_LINES_NUMBER: pane_lines = 4;
pub const PANE_LINES_SIMPLE: pane_lines = 3;
pub const PANE_LINES_HEAVY: pane_lines = 2;
pub const PANE_LINES_DOUBLE: pane_lines = 1;
pub const PANE_LINES_SINGLE: pane_lines = 0;
pub type overlay_mode_cb = Option<
    unsafe extern "C" fn(*mut client, *mut libc::c_void, *mut u_int, *mut u_int) -> *mut screen,
>;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct screen {
    pub title: *mut i8,
    pub path: *mut i8,
    pub titles: *mut screen_titles,
    pub grid: *mut grid,
    pub cx: u_int,
    pub cy: u_int,
    pub cstyle: screen_cursor_style,
    pub default_cstyle: screen_cursor_style,
    pub ccolour: i32,
    pub default_ccolour: i32,
    pub rupper: u_int,
    pub rlower: u_int,
    pub mode: i32,
    pub default_mode: i32,
    pub saved_cx: u_int,
    pub saved_cy: u_int,
    pub saved_grid: *mut grid,
    pub saved_cell: grid_cell,
    pub saved_flags: i32,
    pub tabs: *mut bitstr_t,
    pub sel: *mut screen_sel,
    pub write_list: *mut screen_write_cline,
    pub hyperlinks: *mut hyperlinks,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct screen_write_cline {
    pub data: *mut i8,
    pub items: C2RustUnnamed_10,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_10 {
    pub tqh_first: *mut screen_write_citem,
    pub tqh_last: *mut *mut screen_write_citem,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct screen_write_citem {
    pub x: u_int,
    pub wrapped: i32,
    pub type_0: C2RustUnnamed_12,
    pub used: u_int,
    pub bg: u_int,
    pub gc: grid_cell,
    pub entry: C2RustUnnamed_11,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_11 {
    pub tqe_next: *mut screen_write_citem,
    pub tqe_prev: *mut *mut screen_write_citem,
}
pub type C2RustUnnamed_12 = u32;
pub const CLEAR: C2RustUnnamed_12 = 1;
pub const TEXT: C2RustUnnamed_12 = 0;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct grid {
    pub flags: i32,
    pub sx: u_int,
    pub sy: u_int,
    pub hscrolled: u_int,
    pub hsize: u_int,
    pub hlimit: u_int,
    pub linedata: *mut grid_line,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct grid_line {
    pub celldata: *mut grid_cell_entry,
    pub cellused: u_int,
    pub cellsize: u_int,
    pub extddata: *mut grid_extd_entry,
    pub extdsize: u_int,
    pub flags: i32,
    pub time: time_t,
}
#[derive(Copy, Clone)]
#[repr(C, packed)]
pub struct grid_extd_entry {
    pub data: utf8_char,
    pub attr: u_short,
    pub flags: u_char,
    pub fg: i32,
    pub bg: i32,
    pub us: i32,
    pub link: u_int,
}
pub type utf8_char = u_int;
#[derive(Copy, Clone)]
#[repr(C, packed)]
pub struct grid_cell_entry {
    pub c2rust_unnamed: C2RustUnnamed_13,
    pub flags: u_char,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub union C2RustUnnamed_13 {
    pub offset: u_int,
    pub data: C2RustUnnamed_14,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_14 {
    pub attr: u_char,
    pub fg: u_char,
    pub bg: u_char,
    pub data: u_char,
}
pub type screen_cursor_style = u32;
pub const SCREEN_CURSOR_BAR: screen_cursor_style = 3;
pub const SCREEN_CURSOR_UNDERLINE: screen_cursor_style = 2;
pub const SCREEN_CURSOR_BLOCK: screen_cursor_style = 1;
pub const SCREEN_CURSOR_DEFAULT: screen_cursor_style = 0;
pub type overlay_check_cb = Option<
    unsafe extern "C" fn(
        *mut client,
        *mut libc::c_void,
        u_int,
        u_int,
        u_int,
        *mut overlay_ranges,
    ) -> (),
>;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct overlay_ranges {
    pub px: [u_int; 3],
    pub nx: [u_int; 3],
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct session {
    pub id: u_int,
    pub name: *mut i8,
    pub cwd: *const i8,
    pub creation_time: timeval,
    pub last_attached_time: timeval,
    pub activity_time: timeval,
    pub last_activity_time: timeval,
    pub lock_timer: event,
    pub curw: *mut winlink,
    pub lastw: winlink_stack,
    pub windows: winlinks,
    pub statusat: i32,
    pub statuslines: u_int,
    pub options: *mut options,
    pub flags: i32,
    pub attached: u_int,
    pub tio: *mut termios,
    pub environ: *mut environ,
    pub references: i32,
    pub gentry: C2RustUnnamed_16,
    pub entry: C2RustUnnamed_15,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_15 {
    pub rbe_left: *mut session,
    pub rbe_right: *mut session,
    pub rbe_parent: *mut session,
    pub rbe_color: i32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_16 {
    pub tqe_next: *mut session,
    pub tqe_prev: *mut *mut session,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct winlinks {
    pub rbh_root: *mut winlink,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct winlink {
    pub idx: i32,
    pub session: *mut session,
    pub window: *mut window,
    pub flags: i32,
    pub entry: C2RustUnnamed_19,
    pub wentry: C2RustUnnamed_18,
    pub sentry: C2RustUnnamed_17,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_17 {
    pub tqe_next: *mut winlink,
    pub tqe_prev: *mut *mut winlink,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_18 {
    pub tqe_next: *mut winlink,
    pub tqe_prev: *mut *mut winlink,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_19 {
    pub rbe_left: *mut winlink,
    pub rbe_right: *mut winlink,
    pub rbe_parent: *mut winlink,
    pub rbe_color: i32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct window {
    pub id: u_int,
    pub latest: *mut libc::c_void,
    pub name: *mut i8,
    pub name_event: event,
    pub name_time: timeval,
    pub alerts_timer: event,
    pub offset_timer: event,
    pub activity_time: timeval,
    pub active: *mut window_pane,
    pub last_panes: window_panes,
    pub panes: window_panes,
    pub lastlayout: i32,
    pub layout_root: *mut layout_cell,
    pub saved_layout_root: *mut layout_cell,
    pub old_layout: *mut i8,
    pub sx: u_int,
    pub sy: u_int,
    pub manual_sx: u_int,
    pub manual_sy: u_int,
    pub xpixel: u_int,
    pub ypixel: u_int,
    pub new_sx: u_int,
    pub new_sy: u_int,
    pub new_xpixel: u_int,
    pub new_ypixel: u_int,
    pub fill_character: *mut utf8_data,
    pub flags: i32,
    pub alerts_queued: i32,
    pub alerts_entry: C2RustUnnamed_22,
    pub options: *mut options,
    pub references: u_int,
    pub winlinks: C2RustUnnamed_21,
    pub entry: C2RustUnnamed_20,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_20 {
    pub rbe_left: *mut window,
    pub rbe_right: *mut window,
    pub rbe_parent: *mut window,
    pub rbe_color: i32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_21 {
    pub tqh_first: *mut winlink,
    pub tqh_last: *mut *mut winlink,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_22 {
    pub tqe_next: *mut window,
    pub tqe_prev: *mut *mut window,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct layout_cell {
    pub type_0: layout_type,
    pub parent: *mut layout_cell,
    pub sx: u_int,
    pub sy: u_int,
    pub xoff: u_int,
    pub yoff: u_int,
    pub wp: *mut window_pane,
    pub cells: layout_cells,
    pub entry: C2RustUnnamed_23,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_23 {
    pub tqe_next: *mut layout_cell,
    pub tqe_prev: *mut *mut layout_cell,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct layout_cells {
    pub tqh_first: *mut layout_cell,
    pub tqh_last: *mut *mut layout_cell,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct window_pane {
    pub id: u_int,
    pub active_point: u_int,
    pub window: *mut window,
    pub options: *mut options,
    pub layout_cell: *mut layout_cell,
    pub saved_layout_cell: *mut layout_cell,
    pub sx: u_int,
    pub sy: u_int,
    pub xoff: u_int,
    pub yoff: u_int,
    pub flags: i32,
    pub sb_slider_y: u_int,
    pub sb_slider_h: u_int,
    pub argc: i32,
    pub argv: *mut *mut i8,
    pub shell: *mut i8,
    pub cwd: *mut i8,
    pub pid: pid_t,
    pub tty: [i8; 32],
    pub status: i32,
    pub dead_time: timeval,
    pub fd: i32,
    pub event: *mut bufferevent,
    pub offset: window_pane_offset,
    pub base_offset: size_t,
    pub resize_queue: window_pane_resizes,
    pub resize_timer: event,
    pub ictx: *mut input_ctx,
    pub cached_gc: grid_cell,
    pub cached_active_gc: grid_cell,
    pub palette: colour_palette,
    pub pipe_fd: i32,
    pub pipe_event: *mut bufferevent,
    pub pipe_offset: window_pane_offset,
    pub screen: *mut screen,
    pub base: screen,
    pub status_screen: screen,
    pub status_size: size_t,
    pub modes: C2RustUnnamed_27,
    pub searchstr: *mut i8,
    pub searchregex: i32,
    pub border_gc_set: i32,
    pub border_gc: grid_cell,
    pub control_bg: i32,
    pub control_fg: i32,
    pub scrollbar_style: style,
    pub entry: C2RustUnnamed_26,
    pub sentry: C2RustUnnamed_25,
    pub tree_entry: C2RustUnnamed_24,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_24 {
    pub rbe_left: *mut window_pane,
    pub rbe_right: *mut window_pane,
    pub rbe_parent: *mut window_pane,
    pub rbe_color: i32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_25 {
    pub tqe_next: *mut window_pane,
    pub tqe_prev: *mut *mut window_pane,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_26 {
    pub tqe_next: *mut window_pane,
    pub tqe_prev: *mut *mut window_pane,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct style {
    pub gc: grid_cell,
    pub ignore: i32,
    pub fill: i32,
    pub align: style_align,
    pub list: style_list,
    pub range_type: style_range_type,
    pub range_argument: u_int,
    pub range_string: [i8; 16],
    pub width: i32,
    pub pad: i32,
    pub default_type: style_default_type,
}
pub type style_default_type = u32;
pub const STYLE_DEFAULT_POP: style_default_type = 2;
pub const STYLE_DEFAULT_PUSH: style_default_type = 1;
pub const STYLE_DEFAULT_BASE: style_default_type = 0;
pub type style_range_type = u32;
pub const STYLE_RANGE_USER: style_range_type = 6;
pub const STYLE_RANGE_SESSION: style_range_type = 5;
pub const STYLE_RANGE_WINDOW: style_range_type = 4;
pub const STYLE_RANGE_PANE: style_range_type = 3;
pub const STYLE_RANGE_RIGHT: style_range_type = 2;
pub const STYLE_RANGE_LEFT: style_range_type = 1;
pub const STYLE_RANGE_NONE: style_range_type = 0;
pub type style_list = u32;
pub const STYLE_LIST_RIGHT_MARKER: style_list = 4;
pub const STYLE_LIST_LEFT_MARKER: style_list = 3;
pub const STYLE_LIST_FOCUS: style_list = 2;
pub const STYLE_LIST_ON: style_list = 1;
pub const STYLE_LIST_OFF: style_list = 0;
pub type style_align = u32;
pub const STYLE_ALIGN_ABSOLUTE_CENTRE: style_align = 4;
pub const STYLE_ALIGN_RIGHT: style_align = 3;
pub const STYLE_ALIGN_CENTRE: style_align = 2;
pub const STYLE_ALIGN_LEFT: style_align = 1;
pub const STYLE_ALIGN_DEFAULT: style_align = 0;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_27 {
    pub tqh_first: *mut window_mode_entry,
    pub tqh_last: *mut *mut window_mode_entry,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct window_mode_entry {
    pub wp: *mut window_pane,
    pub swp: *mut window_pane,
    pub mode: *const window_mode,
    pub data: *mut libc::c_void,
    pub screen: *mut screen,
    pub prefix: u_int,
    pub entry: C2RustUnnamed_28,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_28 {
    pub tqe_next: *mut window_mode_entry,
    pub tqe_prev: *mut *mut window_mode_entry,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct window_mode {
    pub name: *const i8,
    pub default_format: *const i8,
    pub init: Option<
        unsafe extern "C" fn(*mut window_mode_entry, *mut cmd_find_state, *mut args) -> *mut screen,
    >,
    pub free: Option<unsafe extern "C" fn(*mut window_mode_entry) -> ()>,
    pub resize: Option<unsafe extern "C" fn(*mut window_mode_entry, u_int, u_int) -> ()>,
    pub update: Option<unsafe extern "C" fn(*mut window_mode_entry) -> ()>,
    pub key: Option<
        unsafe extern "C" fn(
            *mut window_mode_entry,
            *mut client,
            *mut session,
            *mut winlink,
            key_code,
            *mut mouse_event,
        ) -> (),
    >,
    pub key_table: Option<unsafe extern "C" fn(*mut window_mode_entry) -> *const i8>,
    pub command: Option<
        unsafe extern "C" fn(
            *mut window_mode_entry,
            *mut client,
            *mut session,
            *mut winlink,
            *mut args,
            *mut mouse_event,
        ) -> (),
    >,
    pub formats: Option<unsafe extern "C" fn(*mut window_mode_entry, *mut format_tree) -> ()>,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct cmd_find_state {
    pub flags: i32,
    pub current: *mut cmd_find_state,
    pub s: *mut session,
    pub wl: *mut winlink,
    pub w: *mut window,
    pub wp: *mut window_pane,
    pub idx: i32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct window_pane_offset {
    pub used: size_t,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct colour_palette {
    pub fg: i32,
    pub bg: i32,
    pub palette: *mut i32,
    pub default_palette: *mut i32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct window_pane_resizes {
    pub tqh_first: *mut window_pane_resize,
    pub tqh_last: *mut *mut window_pane_resize,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct window_pane_resize {
    pub sx: u_int,
    pub sy: u_int,
    pub osx: u_int,
    pub osy: u_int,
    pub entry: C2RustUnnamed_29,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_29 {
    pub tqe_next: *mut window_pane_resize,
    pub tqe_prev: *mut *mut window_pane_resize,
}
pub type layout_type = u32;
pub const LAYOUT_WINDOWPANE: layout_type = 2;
pub const LAYOUT_TOPBOTTOM: layout_type = 1;
pub const LAYOUT_LEFTRIGHT: layout_type = 0;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct window_panes {
    pub tqh_first: *mut window_pane,
    pub tqh_last: *mut *mut window_pane,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct winlink_stack {
    pub tqh_first: *mut winlink,
    pub tqh_last: *mut *mut winlink,
}
pub type prompt_type = u32;
pub const PROMPT_TYPE_INVALID: prompt_type = 255;
pub const PROMPT_TYPE_WINDOW_TARGET: prompt_type = 3;
pub const PROMPT_TYPE_TARGET: prompt_type = 2;
pub const PROMPT_TYPE_SEARCH: prompt_type = 1;
pub const PROMPT_TYPE_COMMAND: prompt_type = 0;
pub type C2RustUnnamed_30 = u32;
pub const PROMPT_COMMAND: C2RustUnnamed_30 = 1;
pub const PROMPT_ENTRY: C2RustUnnamed_30 = 0;
pub type prompt_free_cb = Option<unsafe extern "C" fn(*mut libc::c_void) -> ()>;
pub type prompt_input_cb =
    Option<unsafe extern "C" fn(*mut client, *mut libc::c_void, *const i8, i32) -> i32>;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct key_table {
    pub name: *const i8,
    pub activity_time: timeval,
    pub key_bindings: key_bindings,
    pub default_key_bindings: key_bindings,
    pub references: u_int,
    pub entry: C2RustUnnamed_31,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_31 {
    pub rbe_left: *mut key_table,
    pub rbe_right: *mut key_table,
    pub rbe_parent: *mut key_table,
    pub rbe_color: i32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct key_bindings {
    pub rbh_root: *mut key_binding,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct key_binding {
    pub key: key_code,
    pub cmdlist: *mut cmd_list,
    pub note: *const i8,
    pub flags: i32,
    pub entry: C2RustUnnamed_32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_32 {
    pub rbe_left: *mut key_binding,
    pub rbe_right: *mut key_binding,
    pub rbe_parent: *mut key_binding,
    pub rbe_color: i32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct cmd_list {
    pub references: i32,
    pub group: u_int,
    pub list: *mut cmds,
}
pub type C2RustUnnamed_33 = u32;
pub const CLIENT_EXIT_DETACH: C2RustUnnamed_33 = 2;
pub const CLIENT_EXIT_SHUTDOWN: C2RustUnnamed_33 = 1;
pub const CLIENT_EXIT_RETURN: C2RustUnnamed_33 = 0;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct status_line {
    pub timer: event,
    pub screen: screen,
    pub active: *mut screen,
    pub references: i32,
    pub style: grid_cell,
    pub entries: [status_line_entry; 5],
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct status_line_entry {
    pub expanded: *mut i8,
    pub ranges: style_ranges,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct style_ranges {
    pub tqh_first: *mut style_range,
    pub tqh_last: *mut *mut style_range,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct style_range {
    pub type_0: style_range_type,
    pub argument: u_int,
    pub string: [i8; 16],
    pub start: u_int,
    pub end: u_int,
    pub entry: C2RustUnnamed_34,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_34 {
    pub tqe_next: *mut style_range,
    pub tqe_prev: *mut *mut style_range,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct tty {
    pub client: *mut client,
    pub start_timer: event,
    pub clipboard_timer: event,
    pub last_requests: time_t,
    pub sx: u_int,
    pub sy: u_int,
    pub xpixel: u_int,
    pub ypixel: u_int,
    pub cx: u_int,
    pub cy: u_int,
    pub cstyle: screen_cursor_style,
    pub ccolour: i32,
    pub oflag: i32,
    pub oox: u_int,
    pub ooy: u_int,
    pub osx: u_int,
    pub osy: u_int,
    pub mode: i32,
    pub fg: i32,
    pub bg: i32,
    pub rlower: u_int,
    pub rupper: u_int,
    pub rleft: u_int,
    pub rright: u_int,
    pub event_in: event,
    pub in_0: *mut evbuffer,
    pub event_out: event,
    pub out: *mut evbuffer,
    pub timer: event,
    pub discarded: size_t,
    pub tio: termios,
    pub cell: grid_cell,
    pub last_cell: grid_cell,
    pub flags: i32,
    pub term: *mut tty_term,
    pub mouse_last_x: u_int,
    pub mouse_last_y: u_int,
    pub mouse_last_b: u_int,
    pub mouse_drag_flag: i32,
    pub mouse_scrolling_flag: i32,
    pub mouse_slider_mpos: i32,
    pub mouse_drag_update: Option<unsafe extern "C" fn(*mut client, *mut mouse_event) -> ()>,
    pub mouse_drag_release: Option<unsafe extern "C" fn(*mut client, *mut mouse_event) -> ()>,
    pub key_timer: event,
    pub key_tree: *mut tty_key,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct tty_term {
    pub name: *mut i8,
    pub tty: *mut tty,
    pub features: i32,
    pub acs: [[i8; 2]; 256],
    pub codes: *mut tty_code,
    pub flags: i32,
    pub entry: C2RustUnnamed_35,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_35 {
    pub le_next: *mut tty_term,
    pub le_prev: *mut *mut tty_term,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct client_windows {
    pub rbh_root: *mut client_window,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct client_window {
    pub window: u_int,
    pub pane: *mut window_pane,
    pub sx: u_int,
    pub sy: u_int,
    pub entry: C2RustUnnamed_36,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_36 {
    pub rbe_left: *mut client_window,
    pub rbe_right: *mut client_window,
    pub rbe_parent: *mut client_window,
    pub rbe_color: i32,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct screen_write_ctx {
    pub wp: *mut window_pane,
    pub s: *mut screen,
    pub flags: i32,
    pub init_ctx_cb: screen_write_init_ctx_cb,
    pub arg: *mut libc::c_void,
    pub item: *mut screen_write_citem,
    pub scrolled: u_int,
    pub bg: u_int,
}
pub type screen_write_init_ctx_cb =
    Option<unsafe extern "C" fn(*mut screen_write_ctx, *mut tty_ctx) -> ()>;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct tty_ctx {
    pub s: *mut screen,
    pub redraw_cb: tty_ctx_redraw_cb,
    pub set_client_cb: tty_ctx_set_client_cb,
    pub arg: *mut libc::c_void,
    pub cell: *const grid_cell,
    pub wrapped: i32,
    pub num: u_int,
    pub ptr: *mut libc::c_void,
    pub ptr2: *mut libc::c_void,
    pub allow_invisible_panes: i32,
    pub ocx: u_int,
    pub ocy: u_int,
    pub orupper: u_int,
    pub orlower: u_int,
    pub xoff: u_int,
    pub yoff: u_int,
    pub rxoff: u_int,
    pub ryoff: u_int,
    pub sx: u_int,
    pub sy: u_int,
    pub bg: u_int,
    pub defaults: grid_cell,
    pub palette: *mut colour_palette,
    pub bigger: i32,
    pub wox: u_int,
    pub woy: u_int,
    pub wsx: u_int,
    pub wsy: u_int,
}
pub type tty_ctx_set_client_cb = Option<unsafe extern "C" fn(*mut tty_ctx, *mut client) -> i32>;
pub type tty_ctx_redraw_cb = Option<unsafe extern "C" fn(*const tty_ctx) -> ()>;
pub type utf8_state = u32;
pub const UTF8_ERROR: utf8_state = 2;
pub const UTF8_DONE: utf8_state = 1;
pub const UTF8_MORE: utf8_state = 0;
pub type box_lines = i32;
pub const BOX_LINES_NONE: box_lines = 6;
pub const BOX_LINES_PADDED: box_lines = 5;
pub const BOX_LINES_ROUNDED: box_lines = 4;
pub const BOX_LINES_SIMPLE: box_lines = 3;
pub const BOX_LINES_HEAVY: box_lines = 2;
pub const BOX_LINES_DOUBLE: box_lines = 1;
pub const BOX_LINES_SINGLE: box_lines = 0;
pub const BOX_LINES_DEFAULT: box_lines = -1;
#[derive(Copy, Clone)]
#[repr(C)]
pub struct menu_item {
    pub name: *const i8,
    pub key: key_code,
    pub command: *const i8,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct menu {
    pub title: *const i8,
    pub items: *mut menu_item,
    pub count: u_int,
    pub width: u_int,
}
#[derive(Copy, Clone)]
#[repr(C)]
pub struct C2RustUnnamed_37 {
    pub tqh_first: *mut screen_write_citem,
    pub tqh_last: *mut *mut screen_write_citem,
}
#[no_mangle]
pub static mut screen_write_citem_freelist: C2RustUnnamed_37 = C2RustUnnamed_37 {
    tqh_first: 0 as *const screen_write_citem as *mut screen_write_citem,
    tqh_last: 0 as *const *mut screen_write_citem as *mut *mut screen_write_citem,
};
extern "C" fn screen_write_get_citem() -> *mut screen_write_citem {
    let mut ci: *mut screen_write_citem = std::ptr::null_mut::<screen_write_citem>();
    ci = unsafe { screen_write_citem_freelist.tqh_first };
    if !ci.is_null() {
        if !(unsafe { (*ci).entry.tqe_next }).is_null() {
            (unsafe { (*(*ci).entry.tqe_next).entry.tqe_prev = (*ci).entry.tqe_prev });
        } else {
            (unsafe { screen_write_citem_freelist.tqh_last = (*ci).entry.tqe_prev });
        }
        (unsafe { *(*ci).entry.tqe_prev = (*ci).entry.tqe_next });
        (unsafe {
            memset(
                ci as *mut libc::c_void,
                0 as i32,
                ::core::mem::size_of::<screen_write_citem>() as u64,
            )
        });
        return ci;
    }
    return (unsafe {
        xcalloc(
            1 as i32 as size_t,
            ::core::mem::size_of::<screen_write_citem>() as u64,
        )
    }) as *mut screen_write_citem;
}
extern "C" fn screen_write_free_citem(mut ci: *mut screen_write_citem) {
    (unsafe { (*ci).entry.tqe_next = std::ptr::null_mut::<screen_write_citem>() });
    (unsafe { (*ci).entry.tqe_prev = screen_write_citem_freelist.tqh_last });
    (unsafe { *screen_write_citem_freelist.tqh_last = ci });
    (unsafe { screen_write_citem_freelist.tqh_last = &mut (*ci).entry.tqe_next });
}
extern "C" fn screen_write_offset_timer(
    mut _fd: i32,
    mut _events: i16,
    mut data: *mut libc::c_void,
) {
    let mut w: *mut window = data as *mut window;
    (unsafe { tty_update_window_offset(w) });
}
extern "C" fn screen_write_set_cursor(mut ctx: *mut screen_write_ctx, mut cx: i32, mut cy: i32) {
    let mut wp: *mut window_pane = unsafe { (*ctx).wp };
    let mut w: *mut window = std::ptr::null_mut::<window>();
    let mut s: *mut screen = unsafe { (*ctx).s };
    let mut tv: timeval = {
        let mut init = timeval {
            tv_sec: 0,
            tv_usec: 10000 as i32 as __suseconds_t,
        };
        init
    };
    if cx != -(1 as i32)
        && cx as u_int == (unsafe { (*s).cx })
        && cy != -(1 as i32)
        && cy as u_int == (unsafe { (*s).cy })
    {
        return;
    }
    if cx != -(1 as i32) {
        if cx as u_int > (unsafe { (*(*s).grid).sx }) {
            cx = (unsafe { (*(*s).grid).sx }).wrapping_sub(1 as i32 as u32) as i32;
        }
        (unsafe { (*s).cx = cx as u_int });
    }
    if cy != -(1 as i32) {
        if cy as u_int > (unsafe { (*(*s).grid).sy }).wrapping_sub(1 as i32 as u32) {
            cy = (unsafe { (*(*s).grid).sy }).wrapping_sub(1 as i32 as u32) as i32;
        }
        (unsafe { (*s).cy = cy as u_int });
    }
    if wp.is_null() {
        return;
    }
    w = unsafe { (*wp).window };
    if (unsafe { event_initialized(&mut (*w).offset_timer) }) == 0 {
        (unsafe {
            event_set(
                &mut (*w).offset_timer,
                -(1 as i32),
                0 as i32 as i16,
                Some(
                    screen_write_offset_timer
                        as unsafe extern "C" fn(i32, i16, *mut libc::c_void) -> (),
                ),
                w as *mut libc::c_void,
            )
        });
    }
    if (unsafe {
        event_pending(
            &mut (*w).offset_timer,
            0x1 as i32 as i16,
            std::ptr::null_mut::<timeval>(),
        )
    }) == 0
    {
        (unsafe { event_add(&mut (*w).offset_timer, &mut tv) });
    }
}
extern "C" fn screen_write_redraw_cb(mut ttyctx: *const tty_ctx) {
    let mut wp: *mut window_pane = (unsafe { (*ttyctx).arg }) as *mut window_pane;
    if !wp.is_null() {
        (unsafe { (*wp).flags |= 0x1 as i32 });
    }
}
extern "C" fn screen_write_set_client_cb(mut ttyctx: *mut tty_ctx, mut c: *mut client) -> i32 {
    let mut wp: *mut window_pane = (unsafe { (*ttyctx).arg }) as *mut window_pane;
    if (unsafe { (*ttyctx).allow_invisible_panes }) != 0 {
        if (unsafe { session_has((*c).session, (*wp).window) }) != 0 {
            return 1 as i32;
        }
        return 0 as i32;
    }
    if (unsafe { (*(*(*c).session).curw).window }) != (unsafe { (*wp).window }) {
        return 0 as i32;
    }
    if (unsafe { (*wp).layout_cell }).is_null() {
        return 0 as i32;
    }
    if (unsafe { (*wp).flags }) & (0x1 as i32 | 0x2 as i32) != 0 {
        return -(1 as i32);
    }
    if (unsafe { (*c).flags }) & 0x20000000 as i32 as u64 != 0 {
        (unsafe {
            log_debug(
                b"%s: adding %%%u to deferred redraw\0" as *const u8 as *const i8,
                (*::core::mem::transmute::<&[u8; 27], &[i8; 27]>(b"screen_write_set_client_cb\0"))
                    .as_ptr(),
                (*wp).id,
            )
        });
        (unsafe { (*wp).flags |= 0x1 as i32 | 0x4000 as i32 });
        return -(1 as i32);
    }
    (unsafe {
        (*ttyctx).bigger = tty_window_offset(
            &mut (*c).tty,
            &mut (*ttyctx).wox,
            &mut (*ttyctx).woy,
            &mut (*ttyctx).wsx,
            &mut (*ttyctx).wsy,
        )
    });
    (unsafe { (*ttyctx).rxoff = (*wp).xoff });
    (unsafe { (*ttyctx).xoff = (*ttyctx).rxoff });
    (unsafe { (*ttyctx).ryoff = (*wp).yoff });
    (unsafe { (*ttyctx).yoff = (*ttyctx).ryoff });
    if (unsafe { status_at_line(c) }) == 0 as i32 {
        (unsafe {
            (*ttyctx).yoff =
                ((*ttyctx).yoff as u32).wrapping_add(status_line_size(c)) as u_int as u_int
        });
    }
    return 1 as i32;
}
extern "C" fn screen_write_initctx(
    mut ctx: *mut screen_write_ctx,
    mut ttyctx: *mut tty_ctx,
    mut sync: i32,
) {
    let mut s: *mut screen = unsafe { (*ctx).s };
    (unsafe {
        memset(
            ttyctx as *mut libc::c_void,
            0 as i32,
            ::core::mem::size_of::<tty_ctx>() as u64,
        )
    });
    (unsafe { (*ttyctx).s = s });
    (unsafe { (*ttyctx).sx = (*(*s).grid).sx });
    (unsafe { (*ttyctx).sy = (*(*s).grid).sy });
    (unsafe { (*ttyctx).ocx = (*s).cx });
    (unsafe { (*ttyctx).ocy = (*s).cy });
    (unsafe { (*ttyctx).orlower = (*s).rlower });
    (unsafe { (*ttyctx).orupper = (*s).rupper });
    (unsafe {
        memcpy(
            &mut (*ttyctx).defaults as *mut grid_cell as *mut libc::c_void,
            &grid_default_cell as *const grid_cell as *const libc::c_void,
            ::core::mem::size_of::<grid_cell>() as u64,
        )
    });
    if unsafe { ((*ctx).init_ctx_cb).is_some() } {
        (unsafe { ((*ctx).init_ctx_cb).expect("non-null function pointer")(ctx, ttyctx) });
        if !(unsafe { (*ttyctx).palette }).is_null() {
            if (unsafe { (*ttyctx).defaults.fg }) == 8 as i32 {
                (unsafe { (*ttyctx).defaults.fg = (*(*ttyctx).palette).fg });
            }
            if (unsafe { (*ttyctx).defaults.bg }) == 8 as i32 {
                (unsafe { (*ttyctx).defaults.bg = (*(*ttyctx).palette).bg });
            }
        }
    } else {
        (unsafe {
            (*ttyctx).redraw_cb =
                Some(screen_write_redraw_cb as unsafe extern "C" fn(*const tty_ctx) -> ())
        });
        if !(unsafe { (*ctx).wp }).is_null() {
            (unsafe { tty_default_colours(&mut (*ttyctx).defaults, (*ctx).wp) });
            (unsafe { (*ttyctx).palette = &mut (*(*ctx).wp).palette });
            (unsafe {
                (*ttyctx).set_client_cb = Some(
                    screen_write_set_client_cb
                        as unsafe extern "C" fn(*mut tty_ctx, *mut client) -> i32,
                )
            });
            (unsafe { (*ttyctx).arg = (*ctx).wp as *mut libc::c_void });
        }
    }
    if !(unsafe { (*ctx).flags }) & 0x1 as i32 != 0 {
        if !(unsafe { (*ctx).wp }).is_null() {
            if (unsafe { (*ctx).wp }) != (unsafe { (*(*(*ctx).wp).window).active }) {
                (unsafe { (*ttyctx).num = 1 as i32 as u_int });
            } else {
                (unsafe { (*ttyctx).num = sync as u_int });
            }
        } else {
            (unsafe { (*ttyctx).num = (0x10 as i32 | sync) as u_int });
        }
        (unsafe {
            tty_write(
                Some(tty_cmd_syncstart as unsafe extern "C" fn(*mut tty, *const tty_ctx) -> ()),
                ttyctx,
            )
        });
        (unsafe { (*ctx).flags |= 0x1 as i32 });
    }
}
#[no_mangle]
pub extern "C" fn screen_write_make_list(mut s: *mut screen) {
    let mut y: u_int = 0;
    (unsafe {
        (*s).write_list = xcalloc(
            (*(*s).grid).sy as size_t,
            ::core::mem::size_of::<screen_write_cline>() as u64,
        ) as *mut screen_write_cline
    });
    y = 0 as i32 as u_int;
    while y < (unsafe { (*(*s).grid).sy }) {
        let fresh0 = unsafe { &mut (*((*s).write_list).offset(y as isize)).items.tqh_first };
        *fresh0 = std::ptr::null_mut::<screen_write_citem>();
        let fresh1 = unsafe { &mut (*((*s).write_list).offset(y as isize)).items.tqh_last };
        *fresh1 = unsafe { &mut (*((*s).write_list).offset(y as isize)).items.tqh_first };
        y = y.wrapping_add(1);
    }
}
#[no_mangle]
pub extern "C" fn screen_write_free_list(mut s: *mut screen) {
    let mut y: u_int = 0;
    y = 0 as i32 as u_int;
    while y < (unsafe { (*(*s).grid).sy }) {
        (unsafe { free((*((*s).write_list).offset(y as isize)).data as *mut libc::c_void) });
        y = y.wrapping_add(1);
    }
    (unsafe { free((*s).write_list as *mut libc::c_void) });
}
extern "C" fn screen_write_init(mut ctx: *mut screen_write_ctx, mut s: *mut screen) {
    (unsafe {
        memset(
            ctx as *mut libc::c_void,
            0 as i32,
            ::core::mem::size_of::<screen_write_ctx>() as u64,
        )
    });
    (unsafe { (*ctx).s = s });
    if (unsafe { (*(*ctx).s).write_list }).is_null() {
        screen_write_make_list(unsafe { (*ctx).s });
    }
    (unsafe { (*ctx).item = screen_write_get_citem() });
    (unsafe { (*ctx).scrolled = 0 as i32 as u_int });
    (unsafe { (*ctx).bg = 8 as i32 as u_int });
}
#[no_mangle]
pub extern "C" fn screen_write_start_pane(
    mut ctx: *mut screen_write_ctx,
    mut wp: *mut window_pane,
    mut s: *mut screen,
) {
    if s.is_null() {
        s = unsafe { (*wp).screen };
    }
    screen_write_init(ctx, s);
    (unsafe { (*ctx).wp = wp });
    if (unsafe { log_get_level() }) != 0 as i32 {
        (unsafe {
            log_debug(
                b"%s: size %ux%u, pane %%%u (at %u,%u)\0" as *const u8 as *const i8,
                (*::core::mem::transmute::<&[u8; 24], &[i8; 24]>(b"screen_write_start_pane\0"))
                    .as_ptr(),
                (*(*(*ctx).s).grid).sx,
                (*(*(*ctx).s).grid).sy,
                (*wp).id,
                (*wp).xoff,
                (*wp).yoff,
            )
        });
    }
}
#[no_mangle]
pub extern "C" fn screen_write_start_callback(
    mut ctx: *mut screen_write_ctx,
    mut s: *mut screen,
    mut cb: screen_write_init_ctx_cb,
    mut arg: *mut libc::c_void,
) {
    screen_write_init(ctx, s);
    (unsafe { (*ctx).init_ctx_cb = cb });
    (unsafe { (*ctx).arg = arg });
    if (unsafe { log_get_level() }) != 0 as i32 {
        (unsafe {
            log_debug(
                b"%s: size %ux%u, with callback\0" as *const u8 as *const i8,
                (*::core::mem::transmute::<&[u8; 28], &[i8; 28]>(b"screen_write_start_callback\0"))
                    .as_ptr(),
                (*(*(*ctx).s).grid).sx,
                (*(*(*ctx).s).grid).sy,
            )
        });
    }
}
#[no_mangle]
pub extern "C" fn screen_write_start(mut ctx: *mut screen_write_ctx, mut s: *mut screen) {
    screen_write_init(ctx, s);
    if (unsafe { log_get_level() }) != 0 as i32 {
        (unsafe {
            log_debug(
                b"%s: size %ux%u, no pane\0" as *const u8 as *const i8,
                (*::core::mem::transmute::<&[u8; 19], &[i8; 19]>(b"screen_write_start\0")).as_ptr(),
                (*(*(*ctx).s).grid).sx,
                (*(*(*ctx).s).grid).sy,
            )
        });
    }
}
#[no_mangle]
pub extern "C" fn screen_write_stop(mut ctx: *mut screen_write_ctx) {
    screen_write_collect_end(ctx);
    screen_write_collect_flush(
        ctx,
        0 as i32,
        (*(unsafe { ::core::mem::transmute::<&[u8; 18], &[i8; 18]>(b"screen_write_stop\0") }))
            .as_ptr(),
    );
    screen_write_free_citem(unsafe { (*ctx).item });
}
#[no_mangle]
pub extern "C" fn screen_write_reset(mut ctx: *mut screen_write_ctx) {
    let mut s: *mut screen = unsafe { (*ctx).s };
    (unsafe { screen_reset_tabs(s) });
    screen_write_scrollregion(
        ctx,
        0 as i32 as u_int,
        (unsafe { (*(*s).grid).sy }).wrapping_sub(1 as i32 as u32),
    );
    (unsafe { (*s).mode = 0x1 as i32 | 0x10 as i32 });
    if (unsafe { options_get_number(global_options, b"extended-keys\0" as *const u8 as *const i8) })
        == 2 as i32 as i64
    {
        (unsafe { (*s).mode = (*s).mode & !(0x8000 as i32 | 0x40000 as i32) | 0x8000 as i32 });
    }
    screen_write_clearscreen(ctx, 8 as i32 as u_int);
    screen_write_set_cursor(ctx, 0 as i32, 0 as i32);
}
#[no_mangle]
pub extern "C" fn screen_write_putc(
    mut ctx: *mut screen_write_ctx,
    mut gcp: *const grid_cell,
    mut ch: u_char,
) {
    let mut gc: grid_cell = grid_cell {
        data: utf8_data {
            data: [0; 21],
            have: 0,
            size: 0,
            width: 0,
        },
        attr: 0,
        flags: 0,
        fg: 0,
        bg: 0,
        us: 0,
        link: 0,
    };
    (unsafe {
        memcpy(
            &mut gc as *mut grid_cell as *mut libc::c_void,
            gcp as *const libc::c_void,
            ::core::mem::size_of::<grid_cell>() as u64,
        )
    });
    (unsafe { utf8_set(&mut gc.data, ch) });
    screen_write_cell(ctx, &mut gc);
}
#[no_mangle]
pub unsafe extern "C" fn screen_write_strlen(mut fmt: *const i8, mut args: ...) -> size_t {
    let mut ap: ::core::ffi::VaListImpl;
    let mut msg: *mut i8 = std::ptr::null_mut::<i8>();
    let mut ud: utf8_data = utf8_data {
        data: [0; 21],
        have: 0,
        size: 0,
        width: 0,
    };
    let mut ptr: *mut u_char = std::ptr::null_mut::<u_char>();
    let mut left: size_t = 0;
    let mut size: size_t = 0 as i32 as size_t;
    let mut more: utf8_state = UTF8_MORE;
    ap = args.clone();
    xvasprintf(&mut msg, fmt, ap.as_va_list());
    ptr = msg as *mut u_char;
    while *ptr as i32 != '\0' as i32 {
        if *ptr as i32 > 0x7f as i32 && utf8_open(&mut ud, *ptr) as u32 == UTF8_MORE as i32 as u32 {
            ptr = ptr.offset(1);
            left = strlen(ptr as *const i8);
            if left < (ud.size as size_t).wrapping_sub(1 as i32 as u64) {
                break;
            }
            loop {
                more = utf8_append(&mut ud, *ptr);
                if more as u32 != UTF8_MORE as i32 as u32 {
                    break;
                }
                ptr = ptr.offset(1);
            }
            ptr = ptr.offset(1);
            if more as u32 == UTF8_DONE as i32 as u32 {
                size = (size as u64).wrapping_add(ud.width as u64) as size_t as size_t;
            }
        } else {
            if *ptr as i32 == '\t' as i32
                || *ptr as i32 > 0x1f as i32 && (*ptr as i32) < 0x7f as i32
            {
                size = size.wrapping_add(1);
            }
            ptr = ptr.offset(1);
        }
    }
    free(msg as *mut libc::c_void);
    return size;
}
#[no_mangle]
pub unsafe extern "C" fn screen_write_text(
    mut ctx: *mut screen_write_ctx,
    mut cx: u_int,
    mut width: u_int,
    mut lines: u_int,
    mut more: i32,
    mut gcp: *const grid_cell,
    mut fmt: *const i8,
    mut args: ...
) -> i32 {
    let mut s: *mut screen = (*ctx).s;
    let mut ap: ::core::ffi::VaListImpl;
    let mut tmp: *mut i8 = std::ptr::null_mut::<i8>();
    let mut cy: u_int = (*s).cy;
    let mut i: u_int = 0;
    let mut end: u_int = 0;
    let mut next: u_int = 0;
    let mut idx: u_int = 0 as i32 as u_int;
    let mut at: u_int = 0;
    let mut left: u_int = 0;
    let mut text: *mut utf8_data = std::ptr::null_mut::<utf8_data>();
    let mut gc: grid_cell = grid_cell {
        data: utf8_data {
            data: [0; 21],
            have: 0,
            size: 0,
            width: 0,
        },
        attr: 0,
        flags: 0,
        fg: 0,
        bg: 0,
        us: 0,
        link: 0,
    };
    memcpy(
        &mut gc as *mut grid_cell as *mut libc::c_void,
        gcp as *const libc::c_void,
        ::core::mem::size_of::<grid_cell>() as u64,
    );
    ap = args.clone();
    xvasprintf(&mut tmp, fmt, ap.as_va_list());
    text = utf8_fromcstr(tmp);
    free(tmp as *mut libc::c_void);
    left = cx.wrapping_add(width).wrapping_sub((*s).cx);
    loop {
        at = 0 as i32 as u_int;
        end = idx;
        while (*text.offset(end as isize)).size as i32 != 0 as i32 {
            if (*text.offset(end as isize)).size as i32 == 1 as i32
                && (*text.offset(end as isize)).data[0 as i32 as usize] as i32 == '\n' as i32
            {
                break;
            }
            if at.wrapping_add((*text.offset(end as isize)).width as u32) > left {
                break;
            }
            at = (at as u32).wrapping_add((*text.offset(end as isize)).width as u32) as u_int
                as u_int;
            end = end.wrapping_add(1);
        }
        if (*text.offset(end as isize)).size as i32 == 0 as i32 {
            next = end;
        } else if (*text.offset(end as isize)).size as i32 == 1 as i32
            && (*text.offset(end as isize)).data[0 as i32 as usize] as i32 == '\n' as i32
        {
            next = end.wrapping_add(1 as i32 as u32);
        } else if (*text.offset(end as isize)).size as i32 == 1 as i32
            && (*text.offset(end as isize)).data[0 as i32 as usize] as i32 == ' ' as i32
        {
            next = end.wrapping_add(1 as i32 as u32);
        } else {
            i = end;
            while i > idx {
                if (*text.offset(i as isize)).size as i32 == 1 as i32
                    && (*text.offset(i as isize)).data[0 as i32 as usize] as i32 == ' ' as i32
                {
                    break;
                }
                i = i.wrapping_sub(1);
            }
            if i != idx {
                next = i.wrapping_add(1 as i32 as u32);
                end = i;
            } else {
                next = end;
            }
        }
        i = idx;
        while i < end {
            utf8_copy(&mut gc.data, &mut *text.offset(i as isize));
            screen_write_cell(ctx, &mut gc);
            i = i.wrapping_add(1);
        }
        idx = next;
        if (*s).cy == cy.wrapping_add(lines).wrapping_sub(1 as i32 as u32)
            || (*text.offset(idx as isize)).size as i32 == 0 as i32
        {
            break;
        }
        screen_write_cursormove(
            ctx,
            cx as i32,
            ((*s).cy).wrapping_add(1 as i32 as u32) as i32,
            0 as i32,
        );
        left = width;
    }
    if (*s).cy == cy.wrapping_add(lines).wrapping_sub(1 as i32 as u32)
        && (more == 0 || (*s).cx == cx.wrapping_add(width))
        || (*text.offset(idx as isize)).size as i32 != 0 as i32
    {
        free(text as *mut libc::c_void);
        return 0 as i32;
    }
    free(text as *mut libc::c_void);
    if more == 0 || (*s).cx == cx.wrapping_add(width) {
        screen_write_cursormove(
            ctx,
            cx as i32,
            ((*s).cy).wrapping_add(1 as i32 as u32) as i32,
            0 as i32,
        );
    }
    return 1 as i32;
}
#[no_mangle]
pub unsafe extern "C" fn screen_write_puts(
    mut ctx: *mut screen_write_ctx,
    mut gcp: *const grid_cell,
    mut fmt: *const i8,
    mut args: ...
) {
    let mut ap: ::core::ffi::VaListImpl;
    ap = args.clone();
    screen_write_vnputs(ctx, -(1 as i32) as ssize_t, gcp, fmt, ap.as_va_list());
}
#[no_mangle]
pub unsafe extern "C" fn screen_write_nputs(
    mut ctx: *mut screen_write_ctx,
    mut maxlen: ssize_t,
    mut gcp: *const grid_cell,
    mut fmt: *const i8,
    mut args: ...
) {
    let mut ap: ::core::ffi::VaListImpl;
    ap = args.clone();
    screen_write_vnputs(ctx, maxlen, gcp, fmt, ap.as_va_list());
}
#[no_mangle]
pub extern "C" fn screen_write_vnputs(
    mut ctx: *mut screen_write_ctx,
    mut maxlen: ssize_t,
    mut gcp: *const grid_cell,
    mut fmt: *const i8,
    mut ap: ::core::ffi::VaList,
) {
    let mut gc: grid_cell = grid_cell {
        data: utf8_data {
            data: [0; 21],
            have: 0,
            size: 0,
            width: 0,
        },
        attr: 0,
        flags: 0,
        fg: 0,
        bg: 0,
        us: 0,
        link: 0,
    };
    let mut ud: *mut utf8_data = &mut gc.data;
    let mut msg: *mut i8 = std::ptr::null_mut::<i8>();
    let mut ptr: *mut u_char = std::ptr::null_mut::<u_char>();
    let mut left: size_t = 0;
    let mut size: size_t = 0 as i32 as size_t;
    let mut more: utf8_state = UTF8_MORE;
    (unsafe {
        memcpy(
            &mut gc as *mut grid_cell as *mut libc::c_void,
            gcp as *const libc::c_void,
            ::core::mem::size_of::<grid_cell>() as u64,
        )
    });
    (unsafe { xvasprintf(&mut msg, fmt, ap.as_va_list()) });
    ptr = msg as *mut u_char;
    while (unsafe { *ptr }) as i32 != '\0' as i32 {
        if (unsafe { *ptr }) as i32 > 0x7f as i32
            && (unsafe { utf8_open(ud, *ptr) }) as u32 == UTF8_MORE as i32 as u32
        {
            ptr = unsafe { ptr.offset(1) };
            left = unsafe { strlen(ptr as *const i8) };
            if left < ((unsafe { (*ud).size }) as size_t).wrapping_sub(1 as i32 as u64) {
                break;
            }
            loop {
                more = unsafe { utf8_append(ud, *ptr) };
                if more as u32 != UTF8_MORE as i32 as u32 {
                    break;
                }
                ptr = unsafe { ptr.offset(1) };
            }
            ptr = unsafe { ptr.offset(1) };
            if more as u32 != UTF8_DONE as i32 as u32 {
                continue;
            }
            if maxlen > 0 as i32 as i64
                && size.wrapping_add((unsafe { (*ud).width }) as u64) > maxlen as size_t
            {
                while size < maxlen as size_t {
                    screen_write_putc(ctx, &mut gc, ' ' as i32 as u_char);
                    size = size.wrapping_add(1);
                }
                break;
            } else {
                size =
                    (size as u64).wrapping_add((unsafe { (*ud).width }) as u64) as size_t as size_t;
                screen_write_cell(ctx, &mut gc);
            }
        } else {
            if maxlen > 0 as i32 as i64 && size.wrapping_add(1 as i32 as u64) > maxlen as size_t {
                break;
            }
            if (unsafe { *ptr }) as i32 == '\u{1}' as i32 {
                gc.attr = (gc.attr as i32 ^ 0x80 as i32) as u_short;
            } else if (unsafe { *ptr }) as i32 == '\n' as i32 {
                screen_write_linefeed(ctx, 0 as i32, 8 as i32 as u_int);
                screen_write_carriagereturn(ctx);
            } else if (unsafe { *ptr }) as i32 == '\t' as i32
                || (unsafe { *ptr }) as i32 > 0x1f as i32
                    && ((unsafe { *ptr }) as i32) < 0x7f as i32
            {
                size = size.wrapping_add(1);
                screen_write_putc(ctx, &mut gc, unsafe { *ptr });
            }
            ptr = unsafe { ptr.offset(1) };
        }
    }
    (unsafe { free(msg as *mut libc::c_void) });
}
#[no_mangle]
pub extern "C" fn screen_write_fast_copy(
    mut ctx: *mut screen_write_ctx,
    mut src: *mut screen,
    mut px: u_int,
    mut py: u_int,
    mut nx: u_int,
    mut ny: u_int,
) {
    let mut s: *mut screen = unsafe { (*ctx).s };
    let mut wp: *mut window_pane = unsafe { (*ctx).wp };
    let mut ttyctx: tty_ctx = tty_ctx {
        s: std::ptr::null_mut::<screen>(),
        redraw_cb: None,
        set_client_cb: None,
        arg: std::ptr::null_mut::<libc::c_void>(),
        cell: std::ptr::null::<grid_cell>(),
        wrapped: 0,
        num: 0,
        ptr: std::ptr::null_mut::<libc::c_void>(),
        ptr2: std::ptr::null_mut::<libc::c_void>(),
        allow_invisible_panes: 0,
        ocx: 0,
        ocy: 0,
        orupper: 0,
        orlower: 0,
        xoff: 0,
        yoff: 0,
        rxoff: 0,
        ryoff: 0,
        sx: 0,
        sy: 0,
        bg: 0,
        defaults: grid_cell {
            data: utf8_data {
                data: [0; 21],
                have: 0,
                size: 0,
                width: 0,
            },
            attr: 0,
            flags: 0,
            fg: 0,
            bg: 0,
            us: 0,
            link: 0,
        },
        palette: std::ptr::null_mut::<colour_palette>(),
        bigger: 0,
        wox: 0,
        woy: 0,
        wsx: 0,
        wsy: 0,
    };
    let mut gd: *mut grid = unsafe { (*src).grid };
    let mut gc: grid_cell = grid_cell {
        data: utf8_data {
            data: [0; 21],
            have: 0,
            size: 0,
            width: 0,
        },
        attr: 0,
        flags: 0,
        fg: 0,
        bg: 0,
        us: 0,
        link: 0,
    };
    let mut xx: u_int = 0;
    let mut yy: u_int = 0;
    let mut cx: u_int = unsafe { (*s).cx };
    let mut cy: u_int = unsafe { (*s).cy };
    if nx == 0 as i32 as u32 || ny == 0 as i32 as u32 {
        return;
    }
    cy = unsafe { (*s).cy };
    yy = py;
    while yy < py.wrapping_add(ny) {
        if yy >= (unsafe { (*gd).hsize }).wrapping_add(unsafe { (*gd).sy }) {
            break;
        }
        (unsafe { (*s).cx = cx });
        if !wp.is_null() {
            screen_write_initctx(ctx, &mut ttyctx, 0 as i32);
        }
        xx = px;
        while xx < px.wrapping_add(nx) {
            if xx >= (unsafe { (*grid_get_line(gd, yy)).cellsize }) {
                break;
            }
            (unsafe { grid_get_cell(gd, xx, yy, &mut gc) });
            if xx.wrapping_add(gc.data.width as u32) > px.wrapping_add(nx) {
                break;
            }
            (unsafe { grid_view_set_cell((*(*ctx).s).grid, (*s).cx, (*s).cy, &mut gc) });
            if !wp.is_null() {
                ttyctx.cell = &mut gc;
                (unsafe {
                    tty_write(
                        Some(tty_cmd_cell as unsafe extern "C" fn(*mut tty, *const tty_ctx) -> ()),
                        &mut ttyctx,
                    )
                });
                ttyctx.ocx = (ttyctx.ocx).wrapping_add(1);
            }
            (unsafe { (*s).cx = ((*s).cx).wrapping_add(1) });
            xx = xx.wrapping_add(1);
        }
        (unsafe { (*s).cy = ((*s).cy).wrapping_add(1) });
        yy = yy.wrapping_add(1);
    }
    (unsafe { (*s).cx = cx });
    (unsafe { (*s).cy = cy });
}
extern "C" fn screen_write_box_border_set(
    mut lines: box_lines,
    mut cell_type: i32,
    mut gc: *mut grid_cell,
) {
    match lines as i32 {
        1 => {
            (unsafe { (*gc).attr = ((*gc).attr as i32 & !(0x80 as i32)) as u_short });
            (unsafe { utf8_copy(&mut (*gc).data, tty_acs_double_borders(cell_type)) });
        }
        2 => {
            (unsafe { (*gc).attr = ((*gc).attr as i32 & !(0x80 as i32)) as u_short });
            (unsafe { utf8_copy(&mut (*gc).data, tty_acs_heavy_borders(cell_type)) });
        }
        4 => {
            (unsafe { (*gc).attr = ((*gc).attr as i32 & !(0x80 as i32)) as u_short });
            (unsafe { utf8_copy(&mut (*gc).data, tty_acs_rounded_borders(cell_type)) });
        }
        3 => {
            (unsafe { (*gc).attr = ((*gc).attr as i32 & !(0x80 as i32)) as u_short });
            (unsafe {
                utf8_set(
                    &mut (*gc).data,
                    (*::core::mem::transmute::<&[u8; 14], &[i8; 14]>(b" |-+++++++++.\0"))
                        [cell_type as usize] as u_char,
                )
            });
        }
        5 => {
            (unsafe { (*gc).attr = ((*gc).attr as i32 & !(0x80 as i32)) as u_short });
            (unsafe {
                utf8_set(
                    &mut (*gc).data,
                    (*::core::mem::transmute::<&[u8; 14], &[i8; 14]>(b"             \0"))
                        [cell_type as usize] as u_char,
                )
            });
        }
        0 | -1 => {
            (unsafe { (*gc).attr = ((*gc).attr as i32 | 0x80 as i32) as u_short });
            (unsafe {
                utf8_set(
                    &mut (*gc).data,
                    (*::core::mem::transmute::<&[u8; 14], &[i8; 14]>(b" xqlkmjwvtun~\0"))
                        [cell_type as usize] as u_char,
                )
            });
        }
        6 | _ => {}
    };
}
#[no_mangle]
pub extern "C" fn screen_write_hline(
    mut ctx: *mut screen_write_ctx,
    mut nx: u_int,
    mut left: i32,
    mut right: i32,
    mut lines: box_lines,
    mut border_gc: *const grid_cell,
) {
    let mut s: *mut screen = unsafe { (*ctx).s };
    let mut gc: grid_cell = grid_cell {
        data: utf8_data {
            data: [0; 21],
            have: 0,
            size: 0,
            width: 0,
        },
        attr: 0,
        flags: 0,
        fg: 0,
        bg: 0,
        us: 0,
        link: 0,
    };
    let mut cx: u_int = 0;
    let mut cy: u_int = 0;
    let mut i: u_int = 0;
    cx = unsafe { (*s).cx };
    cy = unsafe { (*s).cy };
    if !border_gc.is_null() {
        (unsafe {
            memcpy(
                &mut gc as *mut grid_cell as *mut libc::c_void,
                border_gc as *const libc::c_void,
                ::core::mem::size_of::<grid_cell>() as u64,
            )
        });
    } else {
        (unsafe {
            memcpy(
                &mut gc as *mut grid_cell as *mut libc::c_void,
                &grid_default_cell as *const grid_cell as *const libc::c_void,
                ::core::mem::size_of::<grid_cell>() as u64,
            )
        });
    }
    gc.attr = (gc.attr as i32 | 0x80 as i32) as u_short;
    if left != 0 {
        screen_write_box_border_set(lines, 9 as i32, &mut gc);
    } else {
        screen_write_box_border_set(lines, 2 as i32, &mut gc);
    }
    screen_write_cell(ctx, &mut gc);
    screen_write_box_border_set(lines, 2 as i32, &mut gc);
    i = 1 as i32 as u_int;
    while i < nx.wrapping_sub(1 as i32 as u32) {
        screen_write_cell(ctx, &mut gc);
        i = i.wrapping_add(1);
    }
    if right != 0 {
        screen_write_box_border_set(lines, 10 as i32, &mut gc);
    } else {
        screen_write_box_border_set(lines, 2 as i32, &mut gc);
    }
    screen_write_cell(ctx, &mut gc);
    screen_write_set_cursor(ctx, cx as i32, cy as i32);
}
#[no_mangle]
pub extern "C" fn screen_write_vline(
    mut ctx: *mut screen_write_ctx,
    mut ny: u_int,
    mut top: i32,
    mut bottom: i32,
) {
    let mut s: *mut screen = unsafe { (*ctx).s };
    let mut gc: grid_cell = grid_cell {
        data: utf8_data {
            data: [0; 21],
            have: 0,
            size: 0,
            width: 0,
        },
        attr: 0,
        flags: 0,
        fg: 0,
        bg: 0,
        us: 0,
        link: 0,
    };
    let mut cx: u_int = 0;
    let mut cy: u_int = 0;
    let mut i: u_int = 0;
    cx = unsafe { (*s).cx };
    cy = unsafe { (*s).cy };
    (unsafe {
        memcpy(
            &mut gc as *mut grid_cell as *mut libc::c_void,
            &grid_default_cell as *const grid_cell as *const libc::c_void,
            ::core::mem::size_of::<grid_cell>() as u64,
        )
    });
    gc.attr = (gc.attr as i32 | 0x80 as i32) as u_short;
    screen_write_putc(
        ctx,
        &mut gc,
        (if top != 0 { 'w' as i32 } else { 'x' as i32 }) as u_char,
    );
    i = 1 as i32 as u_int;
    while i < ny.wrapping_sub(1 as i32 as u32) {
        screen_write_set_cursor(ctx, cx as i32, cy.wrapping_add(i) as i32);
        screen_write_putc(ctx, &mut gc, 'x' as i32 as u_char);
        i = i.wrapping_add(1);
    }
    screen_write_set_cursor(
        ctx,
        cx as i32,
        cy.wrapping_add(ny).wrapping_sub(1 as i32 as u32) as i32,
    );
    screen_write_putc(
        ctx,
        &mut gc,
        (if bottom != 0 { 'v' as i32 } else { 'x' as i32 }) as u_char,
    );
    screen_write_set_cursor(ctx, cx as i32, cy as i32);
}
#[no_mangle]
pub extern "C" fn screen_write_menu(
    mut ctx: *mut screen_write_ctx,
    mut menu: *mut menu,
    mut choice: i32,
    mut lines: box_lines,
    mut menu_gc: *const grid_cell,
    mut border_gc: *const grid_cell,
    mut choice_gc: *const grid_cell,
) {
    let mut s: *mut screen = unsafe { (*ctx).s };
    let mut default_gc: grid_cell = grid_cell {
        data: utf8_data {
            data: [0; 21],
            have: 0,
            size: 0,
            width: 0,
        },
        attr: 0,
        flags: 0,
        fg: 0,
        bg: 0,
        us: 0,
        link: 0,
    };
    let mut gc: *const grid_cell = &mut default_gc;
    let mut cx: u_int = 0;
    let mut cy: u_int = 0;
    let mut i: u_int = 0;
    let mut j: u_int = 0;
    let mut width: u_int = unsafe { (*menu).width };
    let mut name: *const i8 = std::ptr::null::<i8>();
    cx = unsafe { (*s).cx };
    cy = unsafe { (*s).cy };
    (unsafe {
        memcpy(
            &mut default_gc as *mut grid_cell as *mut libc::c_void,
            menu_gc as *const libc::c_void,
            ::core::mem::size_of::<grid_cell>() as u64,
        )
    });
    screen_write_box(
        ctx,
        (unsafe { (*menu).width }).wrapping_add(4 as i32 as u32),
        (unsafe { (*menu).count }).wrapping_add(2 as i32 as u32),
        lines,
        border_gc,
        unsafe { (*menu).title },
    );
    i = 0 as i32 as u_int;
    while i < (unsafe { (*menu).count }) {
        name = unsafe { (*((*menu).items).offset(i as isize)).name };
        if name.is_null() {
            screen_write_cursormove(
                ctx,
                cx as i32,
                cy.wrapping_add(1 as i32 as u32).wrapping_add(i) as i32,
                0 as i32,
            );
            screen_write_hline(
                ctx,
                width.wrapping_add(4 as i32 as u32),
                1 as i32,
                1 as i32,
                lines,
                border_gc,
            );
        } else {
            if choice >= 0 as i32 && i == choice as u_int && (unsafe { *name }) as i32 != '-' as i32
            {
                gc = choice_gc;
            }
            screen_write_cursormove(
                ctx,
                cx.wrapping_add(1 as i32 as u32) as i32,
                cy.wrapping_add(1 as i32 as u32).wrapping_add(i) as i32,
                0 as i32,
            );
            j = 0 as i32 as u_int;
            while j < width.wrapping_add(2 as i32 as u32) {
                screen_write_putc(ctx, gc, ' ' as i32 as u_char);
                j = j.wrapping_add(1);
            }
            screen_write_cursormove(
                ctx,
                cx.wrapping_add(2 as i32 as u32) as i32,
                cy.wrapping_add(1 as i32 as u32).wrapping_add(i) as i32,
                0 as i32,
            );
            if (unsafe { *name }) as i32 == '-' as i32 {
                default_gc.attr = (default_gc.attr as i32 | 0x2 as i32) as u_short;
                (unsafe {
                    format_draw(
                        ctx,
                        gc,
                        width,
                        name.offset(1 as i32 as isize),
                        std::ptr::null_mut::<style_ranges>(),
                        0 as i32,
                    )
                });
                default_gc.attr = (default_gc.attr as i32 & !(0x2 as i32)) as u_short;
            } else {
                (unsafe {
                    format_draw(
                        ctx,
                        gc,
                        width,
                        name,
                        std::ptr::null_mut::<style_ranges>(),
                        0 as i32,
                    )
                });
                gc = &mut default_gc;
            }
        }
        i = i.wrapping_add(1);
    }
    screen_write_set_cursor(ctx, cx as i32, cy as i32);
}
#[no_mangle]
pub extern "C" fn screen_write_box(
    mut ctx: *mut screen_write_ctx,
    mut nx: u_int,
    mut ny: u_int,
    mut lines: box_lines,
    mut gcp: *const grid_cell,
    mut title: *const i8,
) {
    let mut s: *mut screen = unsafe { (*ctx).s };
    let mut gc: grid_cell = grid_cell {
        data: utf8_data {
            data: [0; 21],
            have: 0,
            size: 0,
            width: 0,
        },
        attr: 0,
        flags: 0,
        fg: 0,
        bg: 0,
        us: 0,
        link: 0,
    };
    let mut cx: u_int = 0;
    let mut cy: u_int = 0;
    let mut i: u_int = 0;
    cx = unsafe { (*s).cx };
    cy = unsafe { (*s).cy };
    if !gcp.is_null() {
        (unsafe {
            memcpy(
                &mut gc as *mut grid_cell as *mut libc::c_void,
                gcp as *const libc::c_void,
                ::core::mem::size_of::<grid_cell>() as u64,
            )
        });
    } else {
        (unsafe {
            memcpy(
                &mut gc as *mut grid_cell as *mut libc::c_void,
                &grid_default_cell as *const grid_cell as *const libc::c_void,
                ::core::mem::size_of::<grid_cell>() as u64,
            )
        });
    }
    gc.attr = (gc.attr as i32 | 0x80 as i32) as u_short;
    gc.flags = (gc.flags as i32 | 0x20 as i32) as u_char;
    screen_write_box_border_set(lines, 3 as i32, &mut gc);
    screen_write_cell(ctx, &mut gc);
    screen_write_box_border_set(lines, 2 as i32, &mut gc);
    i = 1 as i32 as u_int;
    while i < nx.wrapping_sub(1 as i32 as u32) {
        screen_write_cell(ctx, &mut gc);
        i = i.wrapping_add(1);
    }
    screen_write_box_border_set(lines, 4 as i32, &mut gc);
    screen_write_cell(ctx, &mut gc);
    screen_write_set_cursor(
        ctx,
        cx as i32,
        cy.wrapping_add(ny).wrapping_sub(1 as i32 as u32) as i32,
    );
    screen_write_box_border_set(lines, 5 as i32, &mut gc);
    screen_write_cell(ctx, &mut gc);
    screen_write_box_border_set(lines, 2 as i32, &mut gc);
    i = 1 as i32 as u_int;
    while i < nx.wrapping_sub(1 as i32 as u32) {
        screen_write_cell(ctx, &mut gc);
        i = i.wrapping_add(1);
    }
    screen_write_box_border_set(lines, 6 as i32, &mut gc);
    screen_write_cell(ctx, &mut gc);
    screen_write_box_border_set(lines, 1 as i32, &mut gc);
    i = 1 as i32 as u_int;
    while i < ny.wrapping_sub(1 as i32 as u32) {
        screen_write_set_cursor(ctx, cx as i32, cy.wrapping_add(i) as i32);
        screen_write_cell(ctx, &mut gc);
        screen_write_set_cursor(
            ctx,
            cx.wrapping_add(nx).wrapping_sub(1 as i32 as u32) as i32,
            cy.wrapping_add(i) as i32,
        );
        screen_write_cell(ctx, &mut gc);
        i = i.wrapping_add(1);
    }
    if !title.is_null() {
        gc.attr = (gc.attr as i32 & !(0x80 as i32)) as u_short;
        screen_write_cursormove(
            ctx,
            cx.wrapping_add(2 as i32 as u32) as i32,
            cy as i32,
            0 as i32,
        );
        (unsafe {
            format_draw(
                ctx,
                &mut gc,
                nx.wrapping_sub(4 as i32 as u32),
                title,
                std::ptr::null_mut::<style_ranges>(),
                0 as i32,
            )
        });
    }
    screen_write_set_cursor(ctx, cx as i32, cy as i32);
}
#[no_mangle]
pub extern "C" fn screen_write_preview(
    mut ctx: *mut screen_write_ctx,
    mut src: *mut screen,
    mut nx: u_int,
    mut ny: u_int,
) {
    let mut s: *mut screen = unsafe { (*ctx).s };
    let mut gc: grid_cell = grid_cell {
        data: utf8_data {
            data: [0; 21],
            have: 0,
            size: 0,
            width: 0,
        },
        attr: 0,
        flags: 0,
        fg: 0,
        bg: 0,
        us: 0,
        link: 0,
    };
    let mut cx: u_int = 0;
    let mut cy: u_int = 0;
    let mut px: u_int = 0;
    let mut py: u_int = 0;
    cx = unsafe { (*s).cx };
    cy = unsafe { (*s).cy };
    if (unsafe { (*src).mode }) & 0x1 as i32 != 0 {
        px = unsafe { (*src).cx };
        if px < nx.wrapping_div(3 as i32 as u32) {
            px = 0 as i32 as u_int;
        } else {
            px = px.wrapping_sub(nx.wrapping_div(3 as i32 as u32));
        }
        if px.wrapping_add(nx) > (unsafe { (*(*src).grid).sx }) {
            if nx > (unsafe { (*(*src).grid).sx }) {
                px = 0 as i32 as u_int;
            } else {
                px = (unsafe { (*(*src).grid).sx }).wrapping_sub(nx);
            }
        }
        py = unsafe { (*src).cy };
        if py < ny.wrapping_div(3 as i32 as u32) {
            py = 0 as i32 as u_int;
        } else {
            py = py.wrapping_sub(ny.wrapping_div(3 as i32 as u32));
        }
        if py.wrapping_add(ny) > (unsafe { (*(*src).grid).sy }) {
            if ny > (unsafe { (*(*src).grid).sy }) {
                py = 0 as i32 as u_int;
            } else {
                py = (unsafe { (*(*src).grid).sy }).wrapping_sub(ny);
            }
        }
    } else {
        px = 0 as i32 as u_int;
        py = 0 as i32 as u_int;
    }
    screen_write_fast_copy(
        ctx,
        src,
        px,
        (unsafe { (*(*src).grid).hsize }).wrapping_add(py),
        nx,
        ny,
    );
    if (unsafe { (*src).mode }) & 0x1 as i32 != 0 {
        (unsafe { grid_view_get_cell((*src).grid, (*src).cx, (*src).cy, &mut gc) });
        gc.attr = (gc.attr as i32 | 0x10 as i32) as u_short;
        screen_write_set_cursor(
            ctx,
            cx.wrapping_add((unsafe { (*src).cx }).wrapping_sub(px)) as i32,
            cy.wrapping_add((unsafe { (*src).cy }).wrapping_sub(py)) as i32,
        );
        screen_write_cell(ctx, &mut gc);
    }
}
#[no_mangle]
pub extern "C" fn screen_write_mode_set(mut ctx: *mut screen_write_ctx, mut mode: i32) {
    let mut s: *mut screen = unsafe { (*ctx).s };
    (unsafe { (*s).mode |= mode });
    if (unsafe { log_get_level() }) != 0 as i32 {
        (unsafe {
            log_debug(
                b"%s: %s\0" as *const u8 as *const i8,
                (*::core::mem::transmute::<&[u8; 22], &[i8; 22]>(b"screen_write_mode_set\0"))
                    .as_ptr(),
                screen_mode_to_string(mode),
            )
        });
    }
}
#[no_mangle]
pub extern "C" fn screen_write_mode_clear(mut ctx: *mut screen_write_ctx, mut mode: i32) {
    let mut s: *mut screen = unsafe { (*ctx).s };
    (unsafe { (*s).mode &= !mode });
    if (unsafe { log_get_level() }) != 0 as i32 {
        (unsafe {
            log_debug(
                b"%s: %s\0" as *const u8 as *const i8,
                (*::core::mem::transmute::<&[u8; 24], &[i8; 24]>(b"screen_write_mode_clear\0"))
                    .as_ptr(),
                screen_mode_to_string(mode),
            )
        });
    }
}
#[no_mangle]
pub extern "C" fn screen_write_cursorup(mut ctx: *mut screen_write_ctx, mut ny: u_int) {
    let mut s: *mut screen = unsafe { (*ctx).s };
    let mut cx: u_int = unsafe { (*s).cx };
    let mut cy: u_int = unsafe { (*s).cy };
    if ny == 0 as i32 as u32 {
        ny = 1 as i32 as u_int;
    }
    if cy < (unsafe { (*s).rupper }) {
        if ny > cy {
            ny = cy;
        }
    } else if ny > cy.wrapping_sub(unsafe { (*s).rupper }) {
        ny = cy.wrapping_sub(unsafe { (*s).rupper });
    }
    if cx == (unsafe { (*(*s).grid).sx }) {
        cx = cx.wrapping_sub(1);
    }
    cy = (cy as u32).wrapping_sub(ny) as u_int as u_int;
    screen_write_set_cursor(ctx, cx as i32, cy as i32);
}
#[no_mangle]
pub extern "C" fn screen_write_cursordown(mut ctx: *mut screen_write_ctx, mut ny: u_int) {
    let mut s: *mut screen = unsafe { (*ctx).s };
    let mut cx: u_int = unsafe { (*s).cx };
    let mut cy: u_int = unsafe { (*s).cy };
    if ny == 0 as i32 as u32 {
        ny = 1 as i32 as u_int;
    }
    if cy > (unsafe { (*s).rlower }) {
        if ny
            > (unsafe { (*(*s).grid).sy })
                .wrapping_sub(1 as i32 as u32)
                .wrapping_sub(cy)
        {
            ny = (unsafe { (*(*s).grid).sy })
                .wrapping_sub(1 as i32 as u32)
                .wrapping_sub(cy);
        }
    } else if ny > (unsafe { (*s).rlower }).wrapping_sub(cy) {
        ny = (unsafe { (*s).rlower }).wrapping_sub(cy);
    }
    if cx == (unsafe { (*(*s).grid).sx }) {
        cx = cx.wrapping_sub(1);
    } else if ny == 0 as i32 as u32 {
        return;
    }
    cy = (cy as u32).wrapping_add(ny) as u_int as u_int;
    screen_write_set_cursor(ctx, cx as i32, cy as i32);
}
#[no_mangle]
pub extern "C" fn screen_write_cursorright(mut ctx: *mut screen_write_ctx, mut nx: u_int) {
    let mut s: *mut screen = unsafe { (*ctx).s };
    let mut cx: u_int = unsafe { (*s).cx };
    let mut cy: u_int = unsafe { (*s).cy };
    if nx == 0 as i32 as u32 {
        nx = 1 as i32 as u_int;
    }
    if nx
        > (unsafe { (*(*s).grid).sx })
            .wrapping_sub(1 as i32 as u32)
            .wrapping_sub(cx)
    {
        nx = (unsafe { (*(*s).grid).sx })
            .wrapping_sub(1 as i32 as u32)
            .wrapping_sub(cx);
    }
    if nx == 0 as i32 as u32 {
        return;
    }
    cx = (cx as u32).wrapping_add(nx) as u_int as u_int;
    screen_write_set_cursor(ctx, cx as i32, cy as i32);
}
#[no_mangle]
pub extern "C" fn screen_write_cursorleft(mut ctx: *mut screen_write_ctx, mut nx: u_int) {
    let mut s: *mut screen = unsafe { (*ctx).s };
    let mut cx: u_int = unsafe { (*s).cx };
    let mut cy: u_int = unsafe { (*s).cy };
    if nx == 0 as i32 as u32 {
        nx = 1 as i32 as u_int;
    }
    if nx > cx {
        nx = cx;
    }
    if nx == 0 as i32 as u32 {
        return;
    }
    cx = (cx as u32).wrapping_sub(nx) as u_int as u_int;
    screen_write_set_cursor(ctx, cx as i32, cy as i32);
}
#[no_mangle]
pub extern "C" fn screen_write_backspace(mut ctx: *mut screen_write_ctx) {
    let mut s: *mut screen = unsafe { (*ctx).s };
    let mut gl: *mut grid_line = std::ptr::null_mut::<grid_line>();
    let mut cx: u_int = unsafe { (*s).cx };
    let mut cy: u_int = unsafe { (*s).cy };
    if cx == 0 as i32 as u32 {
        if cy == 0 as i32 as u32 {
            return;
        }
        gl = unsafe {
            grid_get_line(
                (*s).grid,
                ((*(*s).grid).hsize)
                    .wrapping_add(cy)
                    .wrapping_sub(1 as i32 as u32),
            )
        };
        if (unsafe { (*gl).flags }) & 0x1 as i32 != 0 {
            cy = cy.wrapping_sub(1);
            cx = (unsafe { (*(*s).grid).sx }).wrapping_sub(1 as i32 as u32);
        }
    } else {
        cx = cx.wrapping_sub(1);
    }
    screen_write_set_cursor(ctx, cx as i32, cy as i32);
}
#[no_mangle]
pub extern "C" fn screen_write_alignmenttest(mut ctx: *mut screen_write_ctx) {
    let mut s: *mut screen = unsafe { (*ctx).s };
    let mut ttyctx: tty_ctx = tty_ctx {
        s: std::ptr::null_mut::<screen>(),
        redraw_cb: None,
        set_client_cb: None,
        arg: std::ptr::null_mut::<libc::c_void>(),
        cell: std::ptr::null::<grid_cell>(),
        wrapped: 0,
        num: 0,
        ptr: std::ptr::null_mut::<libc::c_void>(),
        ptr2: std::ptr::null_mut::<libc::c_void>(),
        allow_invisible_panes: 0,
        ocx: 0,
        ocy: 0,
        orupper: 0,
        orlower: 0,
        xoff: 0,
        yoff: 0,
        rxoff: 0,
        ryoff: 0,
        sx: 0,
        sy: 0,
        bg: 0,
        defaults: grid_cell {
            data: utf8_data {
                data: [0; 21],
                have: 0,
                size: 0,
                width: 0,
            },
            attr: 0,
            flags: 0,
            fg: 0,
            bg: 0,
            us: 0,
            link: 0,
        },
        palette: std::ptr::null_mut::<colour_palette>(),
        bigger: 0,
        wox: 0,
        woy: 0,
        wsx: 0,
        wsy: 0,
    };
    let mut gc: grid_cell = grid_cell {
        data: utf8_data {
            data: [0; 21],
            have: 0,
            size: 0,
            width: 0,
        },
        attr: 0,
        flags: 0,
        fg: 0,
        bg: 0,
        us: 0,
        link: 0,
    };
    let mut xx: u_int = 0;
    let mut yy: u_int = 0;
    (unsafe {
        memcpy(
            &mut gc as *mut grid_cell as *mut libc::c_void,
            &grid_default_cell as *const grid_cell as *const libc::c_void,
            ::core::mem::size_of::<grid_cell>() as u64,
        )
    });
    (unsafe { utf8_set(&mut gc.data, 'E' as i32 as u_char) });
    yy = 0 as i32 as u_int;
    while yy < (unsafe { (*(*s).grid).sy }) {
        xx = 0 as i32 as u_int;
        while xx < (unsafe { (*(*s).grid).sx }) {
            (unsafe { grid_view_set_cell((*s).grid, xx, yy, &mut gc) });
            xx = xx.wrapping_add(1);
        }
        yy = yy.wrapping_add(1);
    }
    screen_write_set_cursor(ctx, 0 as i32, 0 as i32);
    (unsafe { (*s).rupper = 0 as i32 as u_int });
    (unsafe { (*s).rlower = ((*(*s).grid).sy).wrapping_sub(1 as i32 as u32) });
    screen_write_initctx(ctx, &mut ttyctx, 1 as i32);
    screen_write_collect_clear(
        ctx,
        0 as i32 as u_int,
        (unsafe { (*(*s).grid).sy }).wrapping_sub(1 as i32 as u32),
    );
    (unsafe {
        tty_write(
            Some(tty_cmd_alignmenttest as unsafe extern "C" fn(*mut tty, *const tty_ctx) -> ()),
            &mut ttyctx,
        )
    });
}
#[no_mangle]
pub extern "C" fn screen_write_insertcharacter(
    mut ctx: *mut screen_write_ctx,
    mut nx: u_int,
    mut bg: u_int,
) {
    let mut s: *mut screen = unsafe { (*ctx).s };
    let mut ttyctx: tty_ctx = tty_ctx {
        s: std::ptr::null_mut::<screen>(),
        redraw_cb: None,
        set_client_cb: None,
        arg: std::ptr::null_mut::<libc::c_void>(),
        cell: std::ptr::null::<grid_cell>(),
        wrapped: 0,
        num: 0,
        ptr: std::ptr::null_mut::<libc::c_void>(),
        ptr2: std::ptr::null_mut::<libc::c_void>(),
        allow_invisible_panes: 0,
        ocx: 0,
        ocy: 0,
        orupper: 0,
        orlower: 0,
        xoff: 0,
        yoff: 0,
        rxoff: 0,
        ryoff: 0,
        sx: 0,
        sy: 0,
        bg: 0,
        defaults: grid_cell {
            data: utf8_data {
                data: [0; 21],
                have: 0,
                size: 0,
                width: 0,
            },
            attr: 0,
            flags: 0,
            fg: 0,
            bg: 0,
            us: 0,
            link: 0,
        },
        palette: std::ptr::null_mut::<colour_palette>(),
        bigger: 0,
        wox: 0,
        woy: 0,
        wsx: 0,
        wsy: 0,
    };
    if nx == 0 as i32 as u32 {
        nx = 1 as i32 as u_int;
    }
    if nx > (unsafe { (*(*s).grid).sx }).wrapping_sub(unsafe { (*s).cx }) {
        nx = (unsafe { (*(*s).grid).sx }).wrapping_sub(unsafe { (*s).cx });
    }
    if nx == 0 as i32 as u32 {
        return;
    }
    if (unsafe { (*s).cx }) > (unsafe { (*(*s).grid).sx }).wrapping_sub(1 as i32 as u32) {
        return;
    }
    screen_write_initctx(ctx, &mut ttyctx, 0 as i32);
    ttyctx.bg = bg;
    (unsafe { grid_view_insert_cells((*s).grid, (*s).cx, (*s).cy, nx, bg) });
    screen_write_collect_flush(
        ctx,
        0 as i32,
        (*(unsafe {
            ::core::mem::transmute::<&[u8; 29], &[i8; 29]>(b"screen_write_insertcharacter\0")
        }))
        .as_ptr(),
    );
    ttyctx.num = nx;
    (unsafe {
        tty_write(
            Some(tty_cmd_insertcharacter as unsafe extern "C" fn(*mut tty, *const tty_ctx) -> ()),
            &mut ttyctx,
        )
    });
}
#[no_mangle]
pub extern "C" fn screen_write_deletecharacter(
    mut ctx: *mut screen_write_ctx,
    mut nx: u_int,
    mut bg: u_int,
) {
    let mut s: *mut screen = unsafe { (*ctx).s };
    let mut ttyctx: tty_ctx = tty_ctx {
        s: std::ptr::null_mut::<screen>(),
        redraw_cb: None,
        set_client_cb: None,
        arg: std::ptr::null_mut::<libc::c_void>(),
        cell: std::ptr::null::<grid_cell>(),
        wrapped: 0,
        num: 0,
        ptr: std::ptr::null_mut::<libc::c_void>(),
        ptr2: std::ptr::null_mut::<libc::c_void>(),
        allow_invisible_panes: 0,
        ocx: 0,
        ocy: 0,
        orupper: 0,
        orlower: 0,
        xoff: 0,
        yoff: 0,
        rxoff: 0,
        ryoff: 0,
        sx: 0,
        sy: 0,
        bg: 0,
        defaults: grid_cell {
            data: utf8_data {
                data: [0; 21],
                have: 0,
                size: 0,
                width: 0,
            },
            attr: 0,
            flags: 0,
            fg: 0,
            bg: 0,
            us: 0,
            link: 0,
        },
        palette: std::ptr::null_mut::<colour_palette>(),
        bigger: 0,
        wox: 0,
        woy: 0,
        wsx: 0,
        wsy: 0,
    };
    if nx == 0 as i32 as u32 {
        nx = 1 as i32 as u_int;
    }
    if nx > (unsafe { (*(*s).grid).sx }).wrapping_sub(unsafe { (*s).cx }) {
        nx = (unsafe { (*(*s).grid).sx }).wrapping_sub(unsafe { (*s).cx });
    }
    if nx == 0 as i32 as u32 {
        return;
    }
    if (unsafe { (*s).cx }) > (unsafe { (*(*s).grid).sx }).wrapping_sub(1 as i32 as u32) {
        return;
    }
    screen_write_initctx(ctx, &mut ttyctx, 0 as i32);
    ttyctx.bg = bg;
    (unsafe { grid_view_delete_cells((*s).grid, (*s).cx, (*s).cy, nx, bg) });
    screen_write_collect_flush(
        ctx,
        0 as i32,
        (*(unsafe {
            ::core::mem::transmute::<&[u8; 29], &[i8; 29]>(b"screen_write_deletecharacter\0")
        }))
        .as_ptr(),
    );
    ttyctx.num = nx;
    (unsafe {
        tty_write(
            Some(tty_cmd_deletecharacter as unsafe extern "C" fn(*mut tty, *const tty_ctx) -> ()),
            &mut ttyctx,
        )
    });
}
#[no_mangle]
pub extern "C" fn screen_write_clearcharacter(
    mut ctx: *mut screen_write_ctx,
    mut nx: u_int,
    mut bg: u_int,
) {
    let mut s: *mut screen = unsafe { (*ctx).s };
    let mut ttyctx: tty_ctx = tty_ctx {
        s: std::ptr::null_mut::<screen>(),
        redraw_cb: None,
        set_client_cb: None,
        arg: std::ptr::null_mut::<libc::c_void>(),
        cell: std::ptr::null::<grid_cell>(),
        wrapped: 0,
        num: 0,
        ptr: std::ptr::null_mut::<libc::c_void>(),
        ptr2: std::ptr::null_mut::<libc::c_void>(),
        allow_invisible_panes: 0,
        ocx: 0,
        ocy: 0,
        orupper: 0,
        orlower: 0,
        xoff: 0,
        yoff: 0,
        rxoff: 0,
        ryoff: 0,
        sx: 0,
        sy: 0,
        bg: 0,
        defaults: grid_cell {
            data: utf8_data {
                data: [0; 21],
                have: 0,
                size: 0,
                width: 0,
            },
            attr: 0,
            flags: 0,
            fg: 0,
            bg: 0,
            us: 0,
            link: 0,
        },
        palette: std::ptr::null_mut::<colour_palette>(),
        bigger: 0,
        wox: 0,
        woy: 0,
        wsx: 0,
        wsy: 0,
    };
    if nx == 0 as i32 as u32 {
        nx = 1 as i32 as u_int;
    }
    if nx > (unsafe { (*(*s).grid).sx }).wrapping_sub(unsafe { (*s).cx }) {
        nx = (unsafe { (*(*s).grid).sx }).wrapping_sub(unsafe { (*s).cx });
    }
    if nx == 0 as i32 as u32 {
        return;
    }
    if (unsafe { (*s).cx }) > (unsafe { (*(*s).grid).sx }).wrapping_sub(1 as i32 as u32) {
        return;
    }
    screen_write_initctx(ctx, &mut ttyctx, 0 as i32);
    ttyctx.bg = bg;
    (unsafe { grid_view_clear((*s).grid, (*s).cx, (*s).cy, nx, 1 as i32 as u_int, bg) });
    screen_write_collect_flush(
        ctx,
        0 as i32,
        (*(unsafe {
            ::core::mem::transmute::<&[u8; 28], &[i8; 28]>(b"screen_write_clearcharacter\0")
        }))
        .as_ptr(),
    );
    ttyctx.num = nx;
    (unsafe {
        tty_write(
            Some(tty_cmd_clearcharacter as unsafe extern "C" fn(*mut tty, *const tty_ctx) -> ()),
            &mut ttyctx,
        )
    });
}
#[no_mangle]
pub extern "C" fn screen_write_insertline(
    mut ctx: *mut screen_write_ctx,
    mut ny: u_int,
    mut bg: u_int,
) {
    let mut s: *mut screen = unsafe { (*ctx).s };
    let mut gd: *mut grid = unsafe { (*s).grid };
    let mut ttyctx: tty_ctx = tty_ctx {
        s: std::ptr::null_mut::<screen>(),
        redraw_cb: None,
        set_client_cb: None,
        arg: std::ptr::null_mut::<libc::c_void>(),
        cell: std::ptr::null::<grid_cell>(),
        wrapped: 0,
        num: 0,
        ptr: std::ptr::null_mut::<libc::c_void>(),
        ptr2: std::ptr::null_mut::<libc::c_void>(),
        allow_invisible_panes: 0,
        ocx: 0,
        ocy: 0,
        orupper: 0,
        orlower: 0,
        xoff: 0,
        yoff: 0,
        rxoff: 0,
        ryoff: 0,
        sx: 0,
        sy: 0,
        bg: 0,
        defaults: grid_cell {
            data: utf8_data {
                data: [0; 21],
                have: 0,
                size: 0,
                width: 0,
            },
            attr: 0,
            flags: 0,
            fg: 0,
            bg: 0,
            us: 0,
            link: 0,
        },
        palette: std::ptr::null_mut::<colour_palette>(),
        bigger: 0,
        wox: 0,
        woy: 0,
        wsx: 0,
        wsy: 0,
    };
    if ny == 0 as i32 as u32 {
        ny = 1 as i32 as u_int;
    }
    if (unsafe { (*s).cy }) < (unsafe { (*s).rupper })
        || (unsafe { (*s).cy }) > (unsafe { (*s).rlower })
    {
        if ny > (unsafe { (*(*s).grid).sy }).wrapping_sub(unsafe { (*s).cy }) {
            ny = (unsafe { (*(*s).grid).sy }).wrapping_sub(unsafe { (*s).cy });
        }
        if ny == 0 as i32 as u32 {
            return;
        }
        screen_write_initctx(ctx, &mut ttyctx, 1 as i32);
        ttyctx.bg = bg;
        (unsafe { grid_view_insert_lines(gd, (*s).cy, ny, bg) });
        screen_write_collect_flush(
            ctx,
            0 as i32,
            (*(unsafe {
                ::core::mem::transmute::<&[u8; 24], &[i8; 24]>(b"screen_write_insertline\0")
            }))
            .as_ptr(),
        );
        ttyctx.num = ny;
        (unsafe {
            tty_write(
                Some(tty_cmd_insertline as unsafe extern "C" fn(*mut tty, *const tty_ctx) -> ()),
                &mut ttyctx,
            )
        });
        return;
    }
    if ny
        > (unsafe { (*s).rlower })
            .wrapping_add(1 as i32 as u32)
            .wrapping_sub(unsafe { (*s).cy })
    {
        ny = (unsafe { (*s).rlower })
            .wrapping_add(1 as i32 as u32)
            .wrapping_sub(unsafe { (*s).cy });
    }
    if ny == 0 as i32 as u32 {
        return;
    }
    screen_write_initctx(ctx, &mut ttyctx, 1 as i32);
    ttyctx.bg = bg;
    if (unsafe { (*s).cy }) < (unsafe { (*s).rupper })
        || (unsafe { (*s).cy }) > (unsafe { (*s).rlower })
    {
        (unsafe { grid_view_insert_lines(gd, (*s).cy, ny, bg) });
    } else {
        (unsafe { grid_view_insert_lines_region(gd, (*s).rlower, (*s).cy, ny, bg) });
    }
    screen_write_collect_flush(
        ctx,
        0 as i32,
        (*(unsafe {
            ::core::mem::transmute::<&[u8; 24], &[i8; 24]>(b"screen_write_insertline\0")
        }))
        .as_ptr(),
    );
    ttyctx.num = ny;
    (unsafe {
        tty_write(
            Some(tty_cmd_insertline as unsafe extern "C" fn(*mut tty, *const tty_ctx) -> ()),
            &mut ttyctx,
        )
    });
}
#[no_mangle]
pub extern "C" fn screen_write_deleteline(
    mut ctx: *mut screen_write_ctx,
    mut ny: u_int,
    mut bg: u_int,
) {
    let mut s: *mut screen = unsafe { (*ctx).s };
    let mut gd: *mut grid = unsafe { (*s).grid };
    let mut ttyctx: tty_ctx = tty_ctx {
        s: std::ptr::null_mut::<screen>(),
        redraw_cb: None,
        set_client_cb: None,
        arg: std::ptr::null_mut::<libc::c_void>(),
        cell: std::ptr::null::<grid_cell>(),
        wrapped: 0,
        num: 0,
        ptr: std::ptr::null_mut::<libc::c_void>(),
        ptr2: std::ptr::null_mut::<libc::c_void>(),
        allow_invisible_panes: 0,
        ocx: 0,
        ocy: 0,
        orupper: 0,
        orlower: 0,
        xoff: 0,
        yoff: 0,
        rxoff: 0,
        ryoff: 0,
        sx: 0,
        sy: 0,
        bg: 0,
        defaults: grid_cell {
            data: utf8_data {
                data: [0; 21],
                have: 0,
                size: 0,
                width: 0,
            },
            attr: 0,
            flags: 0,
            fg: 0,
            bg: 0,
            us: 0,
            link: 0,
        },
        palette: std::ptr::null_mut::<colour_palette>(),
        bigger: 0,
        wox: 0,
        woy: 0,
        wsx: 0,
        wsy: 0,
    };
    let mut sy: u_int = unsafe { (*(*s).grid).sy };
    if ny == 0 as i32 as u32 {
        ny = 1 as i32 as u_int;
    }
    if (unsafe { (*s).cy }) < (unsafe { (*s).rupper })
        || (unsafe { (*s).cy }) > (unsafe { (*s).rlower })
    {
        if ny > sy.wrapping_sub(unsafe { (*s).cy }) {
            ny = sy.wrapping_sub(unsafe { (*s).cy });
        }
        if ny == 0 as i32 as u32 {
            return;
        }
        screen_write_initctx(ctx, &mut ttyctx, 1 as i32);
        ttyctx.bg = bg;
        (unsafe { grid_view_delete_lines(gd, (*s).cy, ny, bg) });
        screen_write_collect_flush(
            ctx,
            0 as i32,
            (*(unsafe {
                ::core::mem::transmute::<&[u8; 24], &[i8; 24]>(b"screen_write_deleteline\0")
            }))
            .as_ptr(),
        );
        ttyctx.num = ny;
        (unsafe {
            tty_write(
                Some(tty_cmd_deleteline as unsafe extern "C" fn(*mut tty, *const tty_ctx) -> ()),
                &mut ttyctx,
            )
        });
        return;
    }
    if ny
        > (unsafe { (*s).rlower })
            .wrapping_add(1 as i32 as u32)
            .wrapping_sub(unsafe { (*s).cy })
    {
        ny = (unsafe { (*s).rlower })
            .wrapping_add(1 as i32 as u32)
            .wrapping_sub(unsafe { (*s).cy });
    }
    if ny == 0 as i32 as u32 {
        return;
    }
    screen_write_initctx(ctx, &mut ttyctx, 1 as i32);
    ttyctx.bg = bg;
    if (unsafe { (*s).cy }) < (unsafe { (*s).rupper })
        || (unsafe { (*s).cy }) > (unsafe { (*s).rlower })
    {
        (unsafe { grid_view_delete_lines(gd, (*s).cy, ny, bg) });
    } else {
        (unsafe { grid_view_delete_lines_region(gd, (*s).rlower, (*s).cy, ny, bg) });
    }
    screen_write_collect_flush(
        ctx,
        0 as i32,
        (*(unsafe {
            ::core::mem::transmute::<&[u8; 24], &[i8; 24]>(b"screen_write_deleteline\0")
        }))
        .as_ptr(),
    );
    ttyctx.num = ny;
    (unsafe {
        tty_write(
            Some(tty_cmd_deleteline as unsafe extern "C" fn(*mut tty, *const tty_ctx) -> ()),
            &mut ttyctx,
        )
    });
}
#[no_mangle]
pub extern "C" fn screen_write_clearline(mut ctx: *mut screen_write_ctx, mut bg: u_int) {
    let mut s: *mut screen = unsafe { (*ctx).s };
    let mut gl: *mut grid_line = std::ptr::null_mut::<grid_line>();
    let mut sx: u_int = unsafe { (*(*s).grid).sx };
    let mut ci: *mut screen_write_citem = unsafe { (*ctx).item };
    gl = unsafe { grid_get_line((*s).grid, ((*(*s).grid).hsize).wrapping_add((*s).cy)) };
    if (unsafe { (*gl).cellsize }) == 0 as i32 as u32
        && (bg == 8 as i32 as u32 || bg == 9 as i32 as u32)
    {
        return;
    }
    (unsafe {
        grid_view_clear(
            (*s).grid,
            0 as i32 as u_int,
            (*s).cy,
            sx,
            1 as i32 as u_int,
            bg,
        )
    });
    screen_write_collect_clear(ctx, unsafe { (*s).cy }, 1 as i32 as u_int);
    (unsafe { (*ci).x = 0 as i32 as u_int });
    (unsafe { (*ci).used = sx });
    (unsafe { (*ci).type_0 = CLEAR });
    (unsafe { (*ci).bg = bg });
    (unsafe { (*ci).entry.tqe_next = std::ptr::null_mut::<screen_write_citem>() });
    (unsafe {
        (*ci).entry.tqe_prev = (*((*(*ctx).s).write_list).offset((*s).cy as isize))
            .items
            .tqh_last
    });
    let fresh2 = unsafe {
        &mut (*(*((*(*ctx).s).write_list).offset((*s).cy as isize))
            .items
            .tqh_last)
    };
    *fresh2 = ci;
    let fresh3 = unsafe {
        &mut (*((*(*ctx).s).write_list).offset((*s).cy as isize))
            .items
            .tqh_last
    };
    *fresh3 = unsafe { &mut (*ci).entry.tqe_next };
    (unsafe { (*ctx).item = screen_write_get_citem() });
}
#[no_mangle]
pub extern "C" fn screen_write_clearendofline(mut ctx: *mut screen_write_ctx, mut bg: u_int) {
    let mut s: *mut screen = unsafe { (*ctx).s };
    let mut gl: *mut grid_line = std::ptr::null_mut::<grid_line>();
    let mut sx: u_int = unsafe { (*(*s).grid).sx };
    let mut ci: *mut screen_write_citem = unsafe { (*ctx).item };
    let mut before: *mut screen_write_citem = std::ptr::null_mut::<screen_write_citem>();
    if (unsafe { (*s).cx }) == 0 as i32 as u32 {
        screen_write_clearline(ctx, bg);
        return;
    }
    gl = unsafe { grid_get_line((*s).grid, ((*(*s).grid).hsize).wrapping_add((*s).cy)) };
    if (unsafe { (*s).cx }) > sx.wrapping_sub(1 as i32 as u32)
        || (unsafe { (*s).cx }) >= (unsafe { (*gl).cellsize })
            && (bg == 8 as i32 as u32 || bg == 9 as i32 as u32)
    {
        return;
    }
    (unsafe {
        grid_view_clear(
            (*s).grid,
            (*s).cx,
            (*s).cy,
            sx.wrapping_sub((*s).cx),
            1 as i32 as u_int,
            bg,
        )
    });
    before = screen_write_collect_trim(
        ctx,
        unsafe { (*s).cy },
        unsafe { (*s).cx },
        sx.wrapping_sub(unsafe { (*s).cx }),
        std::ptr::null_mut::<i32>(),
    );
    (unsafe { (*ci).x = (*s).cx });
    (unsafe { (*ci).used = sx.wrapping_sub((*s).cx) });
    (unsafe { (*ci).type_0 = CLEAR });
    (unsafe { (*ci).bg = bg });
    if before.is_null() {
        (unsafe { (*ci).entry.tqe_next = std::ptr::null_mut::<screen_write_citem>() });
        (unsafe {
            (*ci).entry.tqe_prev = (*((*(*ctx).s).write_list).offset((*s).cy as isize))
                .items
                .tqh_last
        });
        let fresh4 = unsafe {
            &mut (*(*((*(*ctx).s).write_list).offset((*s).cy as isize))
                .items
                .tqh_last)
        };
        *fresh4 = ci;
        let fresh5 = unsafe {
            &mut (*((*(*ctx).s).write_list).offset((*s).cy as isize))
                .items
                .tqh_last
        };
        *fresh5 = unsafe { &mut (*ci).entry.tqe_next };
    } else {
        (unsafe { (*ci).entry.tqe_prev = (*before).entry.tqe_prev });
        (unsafe { (*ci).entry.tqe_next = before });
        (unsafe { *(*before).entry.tqe_prev = ci });
        (unsafe { (*before).entry.tqe_prev = &mut (*ci).entry.tqe_next });
    }
    (unsafe { (*ctx).item = screen_write_get_citem() });
}
#[no_mangle]
pub extern "C" fn screen_write_clearstartofline(mut ctx: *mut screen_write_ctx, mut bg: u_int) {
    let mut s: *mut screen = unsafe { (*ctx).s };
    let mut sx: u_int = unsafe { (*(*s).grid).sx };
    let mut ci: *mut screen_write_citem = unsafe { (*ctx).item };
    let mut before: *mut screen_write_citem = std::ptr::null_mut::<screen_write_citem>();
    if (unsafe { (*s).cx }) >= sx.wrapping_sub(1 as i32 as u32) {
        screen_write_clearline(ctx, bg);
        return;
    }
    if (unsafe { (*s).cx }) > sx.wrapping_sub(1 as i32 as u32) {
        (unsafe {
            grid_view_clear(
                (*s).grid,
                0 as i32 as u_int,
                (*s).cy,
                sx,
                1 as i32 as u_int,
                bg,
            )
        });
    } else {
        (unsafe {
            grid_view_clear(
                (*s).grid,
                0 as i32 as u_int,
                (*s).cy,
                ((*s).cx).wrapping_add(1 as i32 as u32),
                1 as i32 as u_int,
                bg,
            )
        });
    }
    before = screen_write_collect_trim(
        ctx,
        unsafe { (*s).cy },
        0 as i32 as u_int,
        (unsafe { (*s).cx }).wrapping_add(1 as i32 as u32),
        std::ptr::null_mut::<i32>(),
    );
    (unsafe { (*ci).x = 0 as i32 as u_int });
    (unsafe { (*ci).used = ((*s).cx).wrapping_add(1 as i32 as u32) });
    (unsafe { (*ci).type_0 = CLEAR });
    (unsafe { (*ci).bg = bg });
    if before.is_null() {
        (unsafe { (*ci).entry.tqe_next = std::ptr::null_mut::<screen_write_citem>() });
        (unsafe {
            (*ci).entry.tqe_prev = (*((*(*ctx).s).write_list).offset((*s).cy as isize))
                .items
                .tqh_last
        });
        let fresh6 = unsafe {
            &mut (*(*((*(*ctx).s).write_list).offset((*s).cy as isize))
                .items
                .tqh_last)
        };
        *fresh6 = ci;
        let fresh7 = unsafe {
            &mut (*((*(*ctx).s).write_list).offset((*s).cy as isize))
                .items
                .tqh_last
        };
        *fresh7 = unsafe { &mut (*ci).entry.tqe_next };
    } else {
        (unsafe { (*ci).entry.tqe_prev = (*before).entry.tqe_prev });
        (unsafe { (*ci).entry.tqe_next = before });
        (unsafe { *(*before).entry.tqe_prev = ci });
        (unsafe { (*before).entry.tqe_prev = &mut (*ci).entry.tqe_next });
    }
    (unsafe { (*ctx).item = screen_write_get_citem() });
}
#[no_mangle]
pub extern "C" fn screen_write_cursormove(
    mut ctx: *mut screen_write_ctx,
    mut px: i32,
    mut py: i32,
    mut origin: i32,
) {
    let mut s: *mut screen = unsafe { (*ctx).s };
    if origin != 0 && py != -(1 as i32) && (unsafe { (*s).mode }) & 0x2000 as i32 != 0 {
        if py as u_int > (unsafe { (*s).rlower }).wrapping_sub(unsafe { (*s).rupper }) {
            py = (unsafe { (*s).rlower }) as i32;
        } else {
            py = (py as u32).wrapping_add(unsafe { (*s).rupper }) as i32 as i32;
        }
    }
    if px != -(1 as i32) && px as u_int > (unsafe { (*(*s).grid).sx }).wrapping_sub(1 as i32 as u32)
    {
        px = (unsafe { (*(*s).grid).sx }).wrapping_sub(1 as i32 as u32) as i32;
    }
    if py != -(1 as i32) && py as u_int > (unsafe { (*(*s).grid).sy }).wrapping_sub(1 as i32 as u32)
    {
        py = (unsafe { (*(*s).grid).sy }).wrapping_sub(1 as i32 as u32) as i32;
    }
    (unsafe {
        log_debug(
            b"%s: from %u,%u to %u,%u\0" as *const u8 as *const i8,
            (*::core::mem::transmute::<&[u8; 24], &[i8; 24]>(b"screen_write_cursormove\0"))
                .as_ptr(),
            (*s).cx,
            (*s).cy,
            px,
            py,
        )
    });
    screen_write_set_cursor(ctx, px, py);
}
#[no_mangle]
pub extern "C" fn screen_write_reverseindex(mut ctx: *mut screen_write_ctx, mut bg: u_int) {
    let mut s: *mut screen = unsafe { (*ctx).s };
    let mut ttyctx: tty_ctx = tty_ctx {
        s: std::ptr::null_mut::<screen>(),
        redraw_cb: None,
        set_client_cb: None,
        arg: std::ptr::null_mut::<libc::c_void>(),
        cell: std::ptr::null::<grid_cell>(),
        wrapped: 0,
        num: 0,
        ptr: std::ptr::null_mut::<libc::c_void>(),
        ptr2: std::ptr::null_mut::<libc::c_void>(),
        allow_invisible_panes: 0,
        ocx: 0,
        ocy: 0,
        orupper: 0,
        orlower: 0,
        xoff: 0,
        yoff: 0,
        rxoff: 0,
        ryoff: 0,
        sx: 0,
        sy: 0,
        bg: 0,
        defaults: grid_cell {
            data: utf8_data {
                data: [0; 21],
                have: 0,
                size: 0,
                width: 0,
            },
            attr: 0,
            flags: 0,
            fg: 0,
            bg: 0,
            us: 0,
            link: 0,
        },
        palette: std::ptr::null_mut::<colour_palette>(),
        bigger: 0,
        wox: 0,
        woy: 0,
        wsx: 0,
        wsy: 0,
    };
    if (unsafe { (*s).cy }) == (unsafe { (*s).rupper }) {
        (unsafe { grid_view_scroll_region_down((*s).grid, (*s).rupper, (*s).rlower, bg) });
        screen_write_collect_flush(
            ctx,
            0 as i32,
            (*(unsafe {
                ::core::mem::transmute::<&[u8; 26], &[i8; 26]>(b"screen_write_reverseindex\0")
            }))
            .as_ptr(),
        );
        screen_write_initctx(ctx, &mut ttyctx, 1 as i32);
        ttyctx.bg = bg;
        (unsafe {
            tty_write(
                Some(tty_cmd_reverseindex as unsafe extern "C" fn(*mut tty, *const tty_ctx) -> ()),
                &mut ttyctx,
            )
        });
    } else if (unsafe { (*s).cy }) > 0 as i32 as u32 {
        screen_write_set_cursor(
            ctx,
            -(1 as i32),
            (unsafe { (*s).cy }).wrapping_sub(1 as i32 as u32) as i32,
        );
    }
}
#[no_mangle]
pub extern "C" fn screen_write_scrollregion(
    mut ctx: *mut screen_write_ctx,
    mut rupper: u_int,
    mut rlower: u_int,
) {
    let mut s: *mut screen = unsafe { (*ctx).s };
    if rupper > (unsafe { (*(*s).grid).sy }).wrapping_sub(1 as i32 as u32) {
        rupper = (unsafe { (*(*s).grid).sy }).wrapping_sub(1 as i32 as u32);
    }
    if rlower > (unsafe { (*(*s).grid).sy }).wrapping_sub(1 as i32 as u32) {
        rlower = (unsafe { (*(*s).grid).sy }).wrapping_sub(1 as i32 as u32);
    }
    if rupper >= rlower {
        return;
    }
    screen_write_collect_flush(
        ctx,
        0 as i32,
        (*(unsafe {
            ::core::mem::transmute::<&[u8; 26], &[i8; 26]>(b"screen_write_scrollregion\0")
        }))
        .as_ptr(),
    );
    screen_write_set_cursor(ctx, 0 as i32, 0 as i32);
    (unsafe { (*s).rupper = rupper });
    (unsafe { (*s).rlower = rlower });
}
#[no_mangle]
pub extern "C" fn screen_write_linefeed(
    mut ctx: *mut screen_write_ctx,
    mut wrapped: i32,
    mut bg: u_int,
) {
    let mut s: *mut screen = unsafe { (*ctx).s };
    let mut gd: *mut grid = unsafe { (*s).grid };
    let mut gl: *mut grid_line = std::ptr::null_mut::<grid_line>();
    let mut rupper: u_int = unsafe { (*s).rupper };
    let mut rlower: u_int = unsafe { (*s).rlower };
    gl = unsafe { grid_get_line(gd, ((*gd).hsize).wrapping_add((*s).cy)) };
    if wrapped != 0 {
        (unsafe { (*gl).flags |= 0x1 as i32 });
    }
    (unsafe {
        log_debug(
            b"%s: at %u,%u (region %u-%u)\0" as *const u8 as *const i8,
            (*::core::mem::transmute::<&[u8; 22], &[i8; 22]>(b"screen_write_linefeed\0")).as_ptr(),
            (*s).cx,
            (*s).cy,
            rupper,
            rlower,
        )
    });
    if bg != (unsafe { (*ctx).bg }) {
        screen_write_collect_flush(
            ctx,
            1 as i32,
            (*(unsafe {
                ::core::mem::transmute::<&[u8; 22], &[i8; 22]>(b"screen_write_linefeed\0")
            }))
            .as_ptr(),
        );
        (unsafe { (*ctx).bg = bg });
    }
    if (unsafe { (*s).cy }) == (unsafe { (*s).rlower }) {
        (unsafe { grid_view_scroll_region_up(gd, (*s).rupper, (*s).rlower, bg) });
        screen_write_collect_scroll(ctx, bg);
        (unsafe { (*ctx).scrolled = ((*ctx).scrolled).wrapping_add(1) });
    } else if (unsafe { (*s).cy }) < (unsafe { (*(*s).grid).sy }).wrapping_sub(1 as i32 as u32) {
        screen_write_set_cursor(
            ctx,
            -(1 as i32),
            (unsafe { (*s).cy }).wrapping_add(1 as i32 as u32) as i32,
        );
    }
}
#[no_mangle]
pub extern "C" fn screen_write_scrollup(
    mut ctx: *mut screen_write_ctx,
    mut lines: u_int,
    mut bg: u_int,
) {
    let mut s: *mut screen = unsafe { (*ctx).s };
    let mut gd: *mut grid = unsafe { (*s).grid };
    let mut i: u_int = 0;
    if lines == 0 as i32 as u32 {
        lines = 1 as i32 as u_int;
    } else if lines
        > (unsafe { (*s).rlower })
            .wrapping_sub(unsafe { (*s).rupper })
            .wrapping_add(1 as i32 as u32)
    {
        lines = (unsafe { (*s).rlower })
            .wrapping_sub(unsafe { (*s).rupper })
            .wrapping_add(1 as i32 as u32);
    }
    if bg != (unsafe { (*ctx).bg }) {
        screen_write_collect_flush(
            ctx,
            1 as i32,
            (*(unsafe {
                ::core::mem::transmute::<&[u8; 22], &[i8; 22]>(b"screen_write_scrollup\0")
            }))
            .as_ptr(),
        );
        (unsafe { (*ctx).bg = bg });
    }
    i = 0 as i32 as u_int;
    while i < lines {
        (unsafe { grid_view_scroll_region_up(gd, (*s).rupper, (*s).rlower, bg) });
        screen_write_collect_scroll(ctx, bg);
        i = i.wrapping_add(1);
    }
    (unsafe { (*ctx).scrolled = ((*ctx).scrolled as u32).wrapping_add(lines) as u_int as u_int });
}
#[no_mangle]
pub extern "C" fn screen_write_scrolldown(
    mut ctx: *mut screen_write_ctx,
    mut lines: u_int,
    mut bg: u_int,
) {
    let mut s: *mut screen = unsafe { (*ctx).s };
    let mut gd: *mut grid = unsafe { (*s).grid };
    let mut ttyctx: tty_ctx = tty_ctx {
        s: std::ptr::null_mut::<screen>(),
        redraw_cb: None,
        set_client_cb: None,
        arg: std::ptr::null_mut::<libc::c_void>(),
        cell: std::ptr::null::<grid_cell>(),
        wrapped: 0,
        num: 0,
        ptr: std::ptr::null_mut::<libc::c_void>(),
        ptr2: std::ptr::null_mut::<libc::c_void>(),
        allow_invisible_panes: 0,
        ocx: 0,
        ocy: 0,
        orupper: 0,
        orlower: 0,
        xoff: 0,
        yoff: 0,
        rxoff: 0,
        ryoff: 0,
        sx: 0,
        sy: 0,
        bg: 0,
        defaults: grid_cell {
            data: utf8_data {
                data: [0; 21],
                have: 0,
                size: 0,
                width: 0,
            },
            attr: 0,
            flags: 0,
            fg: 0,
            bg: 0,
            us: 0,
            link: 0,
        },
        palette: std::ptr::null_mut::<colour_palette>(),
        bigger: 0,
        wox: 0,
        woy: 0,
        wsx: 0,
        wsy: 0,
    };
    let mut i: u_int = 0;
    screen_write_initctx(ctx, &mut ttyctx, 1 as i32);
    ttyctx.bg = bg;
    if lines == 0 as i32 as u32 {
        lines = 1 as i32 as u_int;
    } else if lines
        > (unsafe { (*s).rlower })
            .wrapping_sub(unsafe { (*s).rupper })
            .wrapping_add(1 as i32 as u32)
    {
        lines = (unsafe { (*s).rlower })
            .wrapping_sub(unsafe { (*s).rupper })
            .wrapping_add(1 as i32 as u32);
    }
    i = 0 as i32 as u_int;
    while i < lines {
        (unsafe { grid_view_scroll_region_down(gd, (*s).rupper, (*s).rlower, bg) });
        i = i.wrapping_add(1);
    }
    screen_write_collect_flush(
        ctx,
        0 as i32,
        (*(unsafe {
            ::core::mem::transmute::<&[u8; 24], &[i8; 24]>(b"screen_write_scrolldown\0")
        }))
        .as_ptr(),
    );
    ttyctx.num = lines;
    (unsafe {
        tty_write(
            Some(tty_cmd_scrolldown as unsafe extern "C" fn(*mut tty, *const tty_ctx) -> ()),
            &mut ttyctx,
        )
    });
}
#[no_mangle]
pub extern "C" fn screen_write_carriagereturn(mut ctx: *mut screen_write_ctx) {
    screen_write_set_cursor(ctx, 0 as i32, -(1 as i32));
}
#[no_mangle]
pub extern "C" fn screen_write_clearendofscreen(mut ctx: *mut screen_write_ctx, mut bg: u_int) {
    let mut s: *mut screen = unsafe { (*ctx).s };
    let mut gd: *mut grid = unsafe { (*s).grid };
    let mut ttyctx: tty_ctx = tty_ctx {
        s: std::ptr::null_mut::<screen>(),
        redraw_cb: None,
        set_client_cb: None,
        arg: std::ptr::null_mut::<libc::c_void>(),
        cell: std::ptr::null::<grid_cell>(),
        wrapped: 0,
        num: 0,
        ptr: std::ptr::null_mut::<libc::c_void>(),
        ptr2: std::ptr::null_mut::<libc::c_void>(),
        allow_invisible_panes: 0,
        ocx: 0,
        ocy: 0,
        orupper: 0,
        orlower: 0,
        xoff: 0,
        yoff: 0,
        rxoff: 0,
        ryoff: 0,
        sx: 0,
        sy: 0,
        bg: 0,
        defaults: grid_cell {
            data: utf8_data {
                data: [0; 21],
                have: 0,
                size: 0,
                width: 0,
            },
            attr: 0,
            flags: 0,
            fg: 0,
            bg: 0,
            us: 0,
            link: 0,
        },
        palette: std::ptr::null_mut::<colour_palette>(),
        bigger: 0,
        wox: 0,
        woy: 0,
        wsx: 0,
        wsy: 0,
    };
    let mut sx: u_int = unsafe { (*(*s).grid).sx };
    let mut sy: u_int = unsafe { (*(*s).grid).sy };
    screen_write_initctx(ctx, &mut ttyctx, 1 as i32);
    ttyctx.bg = bg;
    if (unsafe { (*s).cx }) == 0 as i32 as u32
        && (unsafe { (*s).cy }) == 0 as i32 as u32
        && (unsafe { (*gd).flags }) & 0x1 as i32 != 0
        && !(unsafe { (*ctx).wp }).is_null()
        && (unsafe {
            options_get_number(
                (*(*ctx).wp).options,
                b"scroll-on-clear\0" as *const u8 as *const i8,
            )
        }) != 0
    {
        (unsafe { grid_view_clear_history(gd, bg) });
    } else {
        if (unsafe { (*s).cx }) <= sx.wrapping_sub(1 as i32 as u32) {
            (unsafe {
                grid_view_clear(
                    gd,
                    (*s).cx,
                    (*s).cy,
                    sx.wrapping_sub((*s).cx),
                    1 as i32 as u_int,
                    bg,
                )
            });
        }
        (unsafe {
            grid_view_clear(
                gd,
                0 as i32 as u_int,
                ((*s).cy).wrapping_add(1 as i32 as u32),
                sx,
                sy.wrapping_sub(((*s).cy).wrapping_add(1 as i32 as u32)),
                bg,
            )
        });
    }
    screen_write_collect_clear(
        ctx,
        (unsafe { (*s).cy }).wrapping_add(1 as i32 as u32),
        sy.wrapping_sub((unsafe { (*s).cy }).wrapping_add(1 as i32 as u32)),
    );
    screen_write_collect_flush(
        ctx,
        0 as i32,
        (*(unsafe {
            ::core::mem::transmute::<&[u8; 30], &[i8; 30]>(b"screen_write_clearendofscreen\0")
        }))
        .as_ptr(),
    );
    (unsafe {
        tty_write(
            Some(tty_cmd_clearendofscreen as unsafe extern "C" fn(*mut tty, *const tty_ctx) -> ()),
            &mut ttyctx,
        )
    });
}
#[no_mangle]
pub extern "C" fn screen_write_clearstartofscreen(mut ctx: *mut screen_write_ctx, mut bg: u_int) {
    let mut s: *mut screen = unsafe { (*ctx).s };
    let mut ttyctx: tty_ctx = tty_ctx {
        s: std::ptr::null_mut::<screen>(),
        redraw_cb: None,
        set_client_cb: None,
        arg: std::ptr::null_mut::<libc::c_void>(),
        cell: std::ptr::null::<grid_cell>(),
        wrapped: 0,
        num: 0,
        ptr: std::ptr::null_mut::<libc::c_void>(),
        ptr2: std::ptr::null_mut::<libc::c_void>(),
        allow_invisible_panes: 0,
        ocx: 0,
        ocy: 0,
        orupper: 0,
        orlower: 0,
        xoff: 0,
        yoff: 0,
        rxoff: 0,
        ryoff: 0,
        sx: 0,
        sy: 0,
        bg: 0,
        defaults: grid_cell {
            data: utf8_data {
                data: [0; 21],
                have: 0,
                size: 0,
                width: 0,
            },
            attr: 0,
            flags: 0,
            fg: 0,
            bg: 0,
            us: 0,
            link: 0,
        },
        palette: std::ptr::null_mut::<colour_palette>(),
        bigger: 0,
        wox: 0,
        woy: 0,
        wsx: 0,
        wsy: 0,
    };
    let mut sx: u_int = unsafe { (*(*s).grid).sx };
    screen_write_initctx(ctx, &mut ttyctx, 1 as i32);
    ttyctx.bg = bg;
    if (unsafe { (*s).cy }) > 0 as i32 as u32 {
        (unsafe {
            grid_view_clear(
                (*s).grid,
                0 as i32 as u_int,
                0 as i32 as u_int,
                sx,
                (*s).cy,
                bg,
            )
        });
    }
    if (unsafe { (*s).cx }) > sx.wrapping_sub(1 as i32 as u32) {
        (unsafe {
            grid_view_clear(
                (*s).grid,
                0 as i32 as u_int,
                (*s).cy,
                sx,
                1 as i32 as u_int,
                bg,
            )
        });
    } else {
        (unsafe {
            grid_view_clear(
                (*s).grid,
                0 as i32 as u_int,
                (*s).cy,
                ((*s).cx).wrapping_add(1 as i32 as u32),
                1 as i32 as u_int,
                bg,
            )
        });
    }
    screen_write_collect_clear(ctx, 0 as i32 as u_int, unsafe { (*s).cy });
    screen_write_collect_flush(
        ctx,
        0 as i32,
        (*(unsafe {
            ::core::mem::transmute::<&[u8; 32], &[i8; 32]>(b"screen_write_clearstartofscreen\0")
        }))
        .as_ptr(),
    );
    (unsafe {
        tty_write(
            Some(
                tty_cmd_clearstartofscreen as unsafe extern "C" fn(*mut tty, *const tty_ctx) -> (),
            ),
            &mut ttyctx,
        )
    });
}
#[no_mangle]
pub extern "C" fn screen_write_clearscreen(mut ctx: *mut screen_write_ctx, mut bg: u_int) {
    let mut s: *mut screen = unsafe { (*ctx).s };
    let mut ttyctx: tty_ctx = tty_ctx {
        s: std::ptr::null_mut::<screen>(),
        redraw_cb: None,
        set_client_cb: None,
        arg: std::ptr::null_mut::<libc::c_void>(),
        cell: std::ptr::null::<grid_cell>(),
        wrapped: 0,
        num: 0,
        ptr: std::ptr::null_mut::<libc::c_void>(),
        ptr2: std::ptr::null_mut::<libc::c_void>(),
        allow_invisible_panes: 0,
        ocx: 0,
        ocy: 0,
        orupper: 0,
        orlower: 0,
        xoff: 0,
        yoff: 0,
        rxoff: 0,
        ryoff: 0,
        sx: 0,
        sy: 0,
        bg: 0,
        defaults: grid_cell {
            data: utf8_data {
                data: [0; 21],
                have: 0,
                size: 0,
                width: 0,
            },
            attr: 0,
            flags: 0,
            fg: 0,
            bg: 0,
            us: 0,
            link: 0,
        },
        palette: std::ptr::null_mut::<colour_palette>(),
        bigger: 0,
        wox: 0,
        woy: 0,
        wsx: 0,
        wsy: 0,
    };
    let mut sx: u_int = unsafe { (*(*s).grid).sx };
    let mut sy: u_int = unsafe { (*(*s).grid).sy };
    screen_write_initctx(ctx, &mut ttyctx, 1 as i32);
    ttyctx.bg = bg;
    if (unsafe { (*(*s).grid).flags }) & 0x1 as i32 != 0
        && !(unsafe { (*ctx).wp }).is_null()
        && (unsafe {
            options_get_number(
                (*(*ctx).wp).options,
                b"scroll-on-clear\0" as *const u8 as *const i8,
            )
        }) != 0
    {
        (unsafe { grid_view_clear_history((*s).grid, bg) });
    } else {
        (unsafe { grid_view_clear((*s).grid, 0 as i32 as u_int, 0 as i32 as u_int, sx, sy, bg) });
    }
    screen_write_collect_clear(ctx, 0 as i32 as u_int, sy);
    (unsafe {
        tty_write(
            Some(tty_cmd_clearscreen as unsafe extern "C" fn(*mut tty, *const tty_ctx) -> ()),
            &mut ttyctx,
        )
    });
}
#[no_mangle]
pub extern "C" fn screen_write_clearhistory(mut ctx: *mut screen_write_ctx) {
    (unsafe { grid_clear_history((*(*ctx).s).grid) });
}
#[no_mangle]
pub extern "C" fn screen_write_fullredraw(mut ctx: *mut screen_write_ctx) {
    let mut ttyctx: tty_ctx = tty_ctx {
        s: std::ptr::null_mut::<screen>(),
        redraw_cb: None,
        set_client_cb: None,
        arg: std::ptr::null_mut::<libc::c_void>(),
        cell: std::ptr::null::<grid_cell>(),
        wrapped: 0,
        num: 0,
        ptr: std::ptr::null_mut::<libc::c_void>(),
        ptr2: std::ptr::null_mut::<libc::c_void>(),
        allow_invisible_panes: 0,
        ocx: 0,
        ocy: 0,
        orupper: 0,
        orlower: 0,
        xoff: 0,
        yoff: 0,
        rxoff: 0,
        ryoff: 0,
        sx: 0,
        sy: 0,
        bg: 0,
        defaults: grid_cell {
            data: utf8_data {
                data: [0; 21],
                have: 0,
                size: 0,
                width: 0,
            },
            attr: 0,
            flags: 0,
            fg: 0,
            bg: 0,
            us: 0,
            link: 0,
        },
        palette: std::ptr::null_mut::<colour_palette>(),
        bigger: 0,
        wox: 0,
        woy: 0,
        wsx: 0,
        wsy: 0,
    };
    screen_write_collect_flush(
        ctx,
        0 as i32,
        (*(unsafe {
            ::core::mem::transmute::<&[u8; 24], &[i8; 24]>(b"screen_write_fullredraw\0")
        }))
        .as_ptr(),
    );
    screen_write_initctx(ctx, &mut ttyctx, 1 as i32);
    if (ttyctx.redraw_cb).is_some() {
        (unsafe { (ttyctx.redraw_cb).expect("non-null function pointer")(&mut ttyctx) });
    }
}
extern "C" fn screen_write_collect_trim(
    mut ctx: *mut screen_write_ctx,
    mut y: u_int,
    mut x: u_int,
    mut used: u_int,
    mut wrapped: *mut i32,
) -> *mut screen_write_citem {
    let mut cl: *mut screen_write_cline =
        (unsafe { &mut *((*(*ctx).s).write_list).offset(y as isize) }) as *mut screen_write_cline;
    let mut ci: *mut screen_write_citem = std::ptr::null_mut::<screen_write_citem>();
    let mut ci2: *mut screen_write_citem = std::ptr::null_mut::<screen_write_citem>();
    let mut tmp: *mut screen_write_citem = std::ptr::null_mut::<screen_write_citem>();
    let mut before: *mut screen_write_citem = std::ptr::null_mut::<screen_write_citem>();
    let mut sx: u_int = x;
    let mut ex: u_int = x.wrapping_add(used).wrapping_sub(1 as i32 as u32);
    let mut csx: u_int = 0;
    let mut cex: u_int = 0;
    if (unsafe { (*cl).items.tqh_first }).is_null() {
        return std::ptr::null_mut::<screen_write_citem>();
    }
    ci = unsafe { (*cl).items.tqh_first };
    while !ci.is_null() && {
        tmp = unsafe { (*ci).entry.tqe_next };
        1 as i32 != 0
    } {
        csx = unsafe { (*ci).x };
        cex = (unsafe { (*ci).x })
            .wrapping_add(unsafe { (*ci).used })
            .wrapping_sub(1 as i32 as u32);
        if cex < sx {
            (unsafe {
                log_debug(
                    b"%s: %p %u-%u before %u-%u\0" as *const u8 as *const i8,
                    (*::core::mem::transmute::<&[u8; 26], &[i8; 26]>(
                        b"screen_write_collect_trim\0",
                    ))
                    .as_ptr(),
                    ci,
                    csx,
                    cex,
                    sx,
                    ex,
                )
            });
        } else if csx > ex {
            (unsafe {
                log_debug(
                    b"%s: %p %u-%u after %u-%u\0" as *const u8 as *const i8,
                    (*::core::mem::transmute::<&[u8; 26], &[i8; 26]>(
                        b"screen_write_collect_trim\0",
                    ))
                    .as_ptr(),
                    ci,
                    csx,
                    cex,
                    sx,
                    ex,
                )
            });
            before = ci;
            break;
        } else if csx >= sx && cex <= ex {
            (unsafe {
                log_debug(
                    b"%s: %p %u-%u inside %u-%u\0" as *const u8 as *const i8,
                    (*::core::mem::transmute::<&[u8; 26], &[i8; 26]>(
                        b"screen_write_collect_trim\0",
                    ))
                    .as_ptr(),
                    ci,
                    csx,
                    cex,
                    sx,
                    ex,
                )
            });
            if !(unsafe { (*ci).entry.tqe_next }).is_null() {
                (unsafe { (*(*ci).entry.tqe_next).entry.tqe_prev = (*ci).entry.tqe_prev });
            } else {
                (unsafe { (*cl).items.tqh_last = (*ci).entry.tqe_prev });
            }
            (unsafe { *(*ci).entry.tqe_prev = (*ci).entry.tqe_next });
            screen_write_free_citem(ci);
            if csx == 0 as i32 as u32 && (unsafe { (*ci).wrapped }) != 0 && !wrapped.is_null() {
                (unsafe { *wrapped = 1 as i32 });
            }
        } else if csx < sx && cex >= sx && cex <= ex {
            (unsafe {
                log_debug(
                    b"%s: %p %u-%u start %u-%u\0" as *const u8 as *const i8,
                    (*::core::mem::transmute::<&[u8; 26], &[i8; 26]>(
                        b"screen_write_collect_trim\0",
                    ))
                    .as_ptr(),
                    ci,
                    csx,
                    cex,
                    sx,
                    ex,
                )
            });
            (unsafe { (*ci).used = sx.wrapping_sub(csx) });
            (unsafe {
                log_debug(
                    b"%s: %p now %u-%u\0" as *const u8 as *const i8,
                    (*::core::mem::transmute::<&[u8; 26], &[i8; 26]>(
                        b"screen_write_collect_trim\0",
                    ))
                    .as_ptr(),
                    ci,
                    (*ci).x,
                    ((*ci).x)
                        .wrapping_add((*ci).used)
                        .wrapping_add(1 as i32 as u32),
                )
            });
        } else if cex > ex && csx >= sx && csx <= ex {
            (unsafe {
                log_debug(
                    b"%s: %p %u-%u end %u-%u\0" as *const u8 as *const i8,
                    (*::core::mem::transmute::<&[u8; 26], &[i8; 26]>(
                        b"screen_write_collect_trim\0",
                    ))
                    .as_ptr(),
                    ci,
                    csx,
                    cex,
                    sx,
                    ex,
                )
            });
            (unsafe { (*ci).x = ex.wrapping_add(1 as i32 as u32) });
            (unsafe { (*ci).used = cex.wrapping_sub(ex) });
            (unsafe {
                log_debug(
                    b"%s: %p now %u-%u\0" as *const u8 as *const i8,
                    (*::core::mem::transmute::<&[u8; 26], &[i8; 26]>(
                        b"screen_write_collect_trim\0",
                    ))
                    .as_ptr(),
                    ci,
                    (*ci).x,
                    ((*ci).x)
                        .wrapping_add((*ci).used)
                        .wrapping_add(1 as i32 as u32),
                )
            });
            before = ci;
            break;
        } else {
            (unsafe {
                log_debug(
                    b"%s: %p %u-%u under %u-%u\0" as *const u8 as *const i8,
                    (*::core::mem::transmute::<&[u8; 26], &[i8; 26]>(
                        b"screen_write_collect_trim\0",
                    ))
                    .as_ptr(),
                    ci,
                    csx,
                    cex,
                    sx,
                    ex,
                )
            });
            ci2 = screen_write_get_citem();
            (unsafe { (*ci2).type_0 = (*ci).type_0 });
            (unsafe { (*ci2).bg = (*ci).bg });
            (unsafe {
                memcpy(
                    &mut (*ci2).gc as *mut grid_cell as *mut libc::c_void,
                    &mut (*ci).gc as *mut grid_cell as *const libc::c_void,
                    ::core::mem::size_of::<grid_cell>() as u64,
                )
            });
            (unsafe { (*ci2).entry.tqe_next = (*ci).entry.tqe_next });
            if !(unsafe { (*ci2).entry.tqe_next }).is_null() {
                (unsafe { (*(*ci2).entry.tqe_next).entry.tqe_prev = &mut (*ci2).entry.tqe_next });
            } else {
                (unsafe { (*cl).items.tqh_last = &mut (*ci2).entry.tqe_next });
            }
            (unsafe { (*ci).entry.tqe_next = ci2 });
            (unsafe { (*ci2).entry.tqe_prev = &mut (*ci).entry.tqe_next });
            (unsafe { (*ci).used = sx.wrapping_sub(csx) });
            (unsafe { (*ci2).x = ex.wrapping_add(1 as i32 as u32) });
            (unsafe { (*ci2).used = cex.wrapping_sub(ex) });
            (unsafe {
                log_debug(
                    b"%s: %p now %u-%u (%p) and %u-%u (%p)\0" as *const u8 as *const i8,
                    (*::core::mem::transmute::<&[u8; 26], &[i8; 26]>(
                        b"screen_write_collect_trim\0",
                    ))
                    .as_ptr(),
                    ci,
                    (*ci).x,
                    ((*ci).x)
                        .wrapping_add((*ci).used)
                        .wrapping_sub(1 as i32 as u32),
                    ci,
                    (*ci2).x,
                    ((*ci2).x)
                        .wrapping_add((*ci2).used)
                        .wrapping_sub(1 as i32 as u32),
                    ci2,
                )
            });
            before = ci2;
            break;
        }
        ci = tmp;
    }
    return before;
}
extern "C" fn screen_write_collect_clear(
    mut ctx: *mut screen_write_ctx,
    mut y: u_int,
    mut n: u_int,
) {
    let mut cl: *mut screen_write_cline = std::ptr::null_mut::<screen_write_cline>();
    let mut i: u_int = 0;
    i = y;
    while i < y.wrapping_add(n) {
        cl = (unsafe { &mut *((*(*ctx).s).write_list).offset(i as isize) })
            as *mut screen_write_cline;
        if !(unsafe { (*cl).items.tqh_first }).is_null() {
            (unsafe { *screen_write_citem_freelist.tqh_last = (*cl).items.tqh_first });
            (unsafe {
                (*(*cl).items.tqh_first).entry.tqe_prev = screen_write_citem_freelist.tqh_last
            });
            (unsafe { screen_write_citem_freelist.tqh_last = (*cl).items.tqh_last });
            (unsafe { (*cl).items.tqh_first = std::ptr::null_mut::<screen_write_citem>() });
            (unsafe { (*cl).items.tqh_last = &mut (*cl).items.tqh_first });
        }
        i = i.wrapping_add(1);
    }
}
extern "C" fn screen_write_collect_scroll(mut ctx: *mut screen_write_ctx, mut bg: u_int) {
    let mut s: *mut screen = unsafe { (*ctx).s };
    let mut cl: *mut screen_write_cline = std::ptr::null_mut::<screen_write_cline>();
    let mut y: u_int = 0;
    let mut saved: *mut i8 = std::ptr::null_mut::<i8>();
    let mut ci: *mut screen_write_citem = std::ptr::null_mut::<screen_write_citem>();
    (unsafe {
        log_debug(
            b"%s: at %u,%u (region %u-%u)\0" as *const u8 as *const i8,
            (*::core::mem::transmute::<&[u8; 28], &[i8; 28]>(b"screen_write_collect_scroll\0"))
                .as_ptr(),
            (*s).cx,
            (*s).cy,
            (*s).rupper,
            (*s).rlower,
        )
    });
    screen_write_collect_clear(ctx, unsafe { (*s).rupper }, 1 as i32 as u_int);
    saved = unsafe { (*((*(*ctx).s).write_list).offset((*s).rupper as isize)).data };
    y = unsafe { (*s).rupper };
    while y < (unsafe { (*s).rlower }) {
        cl = (unsafe {
            &mut *((*(*ctx).s).write_list).offset(y.wrapping_add(1 as i32 as u32) as isize)
        }) as *mut screen_write_cline;
        if !(unsafe { (*cl).items.tqh_first }).is_null() {
            let fresh8 = unsafe {
                &mut (*(*((*(*ctx).s).write_list).offset(y as isize))
                    .items
                    .tqh_last)
            };
            *fresh8 = unsafe { (*cl).items.tqh_first };
            (unsafe {
                (*(*cl).items.tqh_first).entry.tqe_prev = (*((*(*ctx).s).write_list)
                    .offset(y as isize))
                .items
                .tqh_last
            });
            let fresh9 = unsafe {
                &mut (*((*(*ctx).s).write_list).offset(y as isize))
                    .items
                    .tqh_last
            };
            *fresh9 = unsafe { (*cl).items.tqh_last };
            (unsafe { (*cl).items.tqh_first = std::ptr::null_mut::<screen_write_citem>() });
            (unsafe { (*cl).items.tqh_last = &mut (*cl).items.tqh_first });
        }
        let fresh10 = unsafe { &mut (*((*(*ctx).s).write_list).offset(y as isize)).data };
        *fresh10 = unsafe { (*cl).data };
        y = y.wrapping_add(1);
    }
    let fresh11 = unsafe { &mut (*((*(*ctx).s).write_list).offset((*s).rlower as isize)).data };
    *fresh11 = saved;
    ci = screen_write_get_citem();
    (unsafe { (*ci).x = 0 as i32 as u_int });
    (unsafe { (*ci).used = (*(*s).grid).sx });
    (unsafe { (*ci).type_0 = CLEAR });
    (unsafe { (*ci).bg = bg });
    (unsafe { (*ci).entry.tqe_next = std::ptr::null_mut::<screen_write_citem>() });
    (unsafe {
        (*ci).entry.tqe_prev = (*((*(*ctx).s).write_list).offset((*s).rlower as isize))
            .items
            .tqh_last
    });
    let fresh12 = unsafe {
        &mut (*(*((*(*ctx).s).write_list).offset((*s).rlower as isize))
            .items
            .tqh_last)
    };
    *fresh12 = ci;
    let fresh13 = unsafe {
        &mut (*((*(*ctx).s).write_list).offset((*s).rlower as isize))
            .items
            .tqh_last
    };
    *fresh13 = unsafe { &mut (*ci).entry.tqe_next };
}
extern "C" fn screen_write_collect_flush(
    mut ctx: *mut screen_write_ctx,
    mut scroll_only: i32,
    mut from: *const i8,
) {
    let mut s: *mut screen = unsafe { (*ctx).s };
    let mut ci: *mut screen_write_citem = std::ptr::null_mut::<screen_write_citem>();
    let mut tmp: *mut screen_write_citem = std::ptr::null_mut::<screen_write_citem>();
    let mut cl: *mut screen_write_cline = std::ptr::null_mut::<screen_write_cline>();
    let mut y: u_int = 0;
    let mut cx: u_int = 0;
    let mut cy: u_int = 0;
    let mut last: u_int = 0;
    let mut items: u_int = 0 as i32 as u_int;
    let mut ttyctx: tty_ctx = tty_ctx {
        s: std::ptr::null_mut::<screen>(),
        redraw_cb: None,
        set_client_cb: None,
        arg: std::ptr::null_mut::<libc::c_void>(),
        cell: std::ptr::null::<grid_cell>(),
        wrapped: 0,
        num: 0,
        ptr: std::ptr::null_mut::<libc::c_void>(),
        ptr2: std::ptr::null_mut::<libc::c_void>(),
        allow_invisible_panes: 0,
        ocx: 0,
        ocy: 0,
        orupper: 0,
        orlower: 0,
        xoff: 0,
        yoff: 0,
        rxoff: 0,
        ryoff: 0,
        sx: 0,
        sy: 0,
        bg: 0,
        defaults: grid_cell {
            data: utf8_data {
                data: [0; 21],
                have: 0,
                size: 0,
                width: 0,
            },
            attr: 0,
            flags: 0,
            fg: 0,
            bg: 0,
            us: 0,
            link: 0,
        },
        palette: std::ptr::null_mut::<colour_palette>(),
        bigger: 0,
        wox: 0,
        woy: 0,
        wsx: 0,
        wsy: 0,
    };
    if (unsafe { (*ctx).scrolled }) != 0 as i32 as u32 {
        (unsafe {
            log_debug(
                b"%s: scrolled %u (region %u-%u)\0" as *const u8 as *const i8,
                (*::core::mem::transmute::<&[u8; 27], &[i8; 27]>(b"screen_write_collect_flush\0"))
                    .as_ptr(),
                (*ctx).scrolled,
                (*s).rupper,
                (*s).rlower,
            )
        });
        if (unsafe { (*ctx).scrolled })
            > (unsafe { (*s).rlower })
                .wrapping_sub(unsafe { (*s).rupper })
                .wrapping_add(1 as i32 as u32)
        {
            (unsafe {
                (*ctx).scrolled = ((*s).rlower)
                    .wrapping_sub((*s).rupper)
                    .wrapping_add(1 as i32 as u32)
            });
        }
        screen_write_initctx(ctx, &mut ttyctx, 1 as i32);
        ttyctx.num = unsafe { (*ctx).scrolled };
        ttyctx.bg = unsafe { (*ctx).bg };
        (unsafe {
            tty_write(
                Some(tty_cmd_scrollup as unsafe extern "C" fn(*mut tty, *const tty_ctx) -> ()),
                &mut ttyctx,
            )
        });
        if !(unsafe { (*ctx).wp }).is_null() {
            (unsafe { (*(*ctx).wp).flags |= 0x4000 as i32 });
        }
    }
    (unsafe { (*ctx).scrolled = 0 as i32 as u_int });
    (unsafe { (*ctx).bg = 8 as i32 as u_int });
    if scroll_only != 0 {
        return;
    }
    cx = unsafe { (*s).cx };
    cy = unsafe { (*s).cy };
    y = 0 as i32 as u_int;
    while y < (unsafe { (*(*s).grid).sy }) {
        cl = (unsafe { &mut *((*(*ctx).s).write_list).offset(y as isize) })
            as *mut screen_write_cline;
        last = (2147483647 as i32 as u32)
            .wrapping_mul(2 as u32)
            .wrapping_add(1 as u32);
        ci = unsafe { (*cl).items.tqh_first };
        while !ci.is_null() && {
            tmp = unsafe { (*ci).entry.tqe_next };
            1 as i32 != 0
        } {
            if last
                != (2147483647 as i32 as u32)
                    .wrapping_mul(2 as u32)
                    .wrapping_add(1 as u32)
                && (unsafe { (*ci).x }) <= last
            {
                (unsafe {
                    fatalx(
                        b"collect list not in order: %u <= %u\0" as *const u8 as *const i8,
                        (*ci).x,
                        last,
                    )
                });
            }
            screen_write_set_cursor(ctx, (unsafe { (*ci).x }) as i32, y as i32);
            if (unsafe { (*ci).type_0 }) as u32 == CLEAR as i32 as u32 {
                screen_write_initctx(ctx, &mut ttyctx, 1 as i32);
                ttyctx.bg = unsafe { (*ci).bg };
                ttyctx.num = unsafe { (*ci).used };
                (unsafe {
                    tty_write(
                        Some(
                            tty_cmd_clearcharacter
                                as unsafe extern "C" fn(*mut tty, *const tty_ctx) -> (),
                        ),
                        &mut ttyctx,
                    )
                });
            } else {
                screen_write_initctx(ctx, &mut ttyctx, 0 as i32);
                ttyctx.cell = unsafe { &mut (*ci).gc };
                ttyctx.wrapped = unsafe { (*ci).wrapped };
                ttyctx.ptr =
                    (unsafe { ((*cl).data).offset((*ci).x as isize) }) as *mut libc::c_void;
                ttyctx.num = unsafe { (*ci).used };
                (unsafe {
                    tty_write(
                        Some(tty_cmd_cells as unsafe extern "C" fn(*mut tty, *const tty_ctx) -> ()),
                        &mut ttyctx,
                    )
                });
            }
            items = items.wrapping_add(1);
            if !(unsafe { (*ci).entry.tqe_next }).is_null() {
                (unsafe { (*(*ci).entry.tqe_next).entry.tqe_prev = (*ci).entry.tqe_prev });
            } else {
                (unsafe { (*cl).items.tqh_last = (*ci).entry.tqe_prev });
            }
            (unsafe { *(*ci).entry.tqe_prev = (*ci).entry.tqe_next });
            screen_write_free_citem(ci);
            last = unsafe { (*ci).x };
            ci = tmp;
        }
        y = y.wrapping_add(1);
    }
    (unsafe { (*s).cx = cx });
    (unsafe { (*s).cy = cy });
    (unsafe {
        log_debug(
            b"%s: flushed %u items (%s)\0" as *const u8 as *const i8,
            (*::core::mem::transmute::<&[u8; 27], &[i8; 27]>(b"screen_write_collect_flush\0"))
                .as_ptr(),
            items,
            from,
        )
    });
}
#[no_mangle]
pub extern "C" fn screen_write_collect_end(mut ctx: *mut screen_write_ctx) {
    let mut s: *mut screen = unsafe { (*ctx).s };
    let mut ci: *mut screen_write_citem = unsafe { (*ctx).item };
    let mut before: *mut screen_write_citem = std::ptr::null_mut::<screen_write_citem>();
    let mut cl: *mut screen_write_cline =
        (unsafe { &mut *((*s).write_list).offset((*s).cy as isize) }) as *mut screen_write_cline;
    let mut gc: grid_cell = grid_cell {
        data: utf8_data {
            data: [0; 21],
            have: 0,
            size: 0,
            width: 0,
        },
        attr: 0,
        flags: 0,
        fg: 0,
        bg: 0,
        us: 0,
        link: 0,
    };
    let mut xx: u_int = 0;
    let mut wrapped: i32 = unsafe { (*ci).wrapped };
    if (unsafe { (*ci).used }) == 0 as i32 as u32 {
        return;
    }
    before = screen_write_collect_trim(
        ctx,
        unsafe { (*s).cy },
        unsafe { (*s).cx },
        unsafe { (*ci).used },
        &mut wrapped,
    );
    (unsafe { (*ci).x = (*s).cx });
    (unsafe { (*ci).wrapped = wrapped });
    if before.is_null() {
        (unsafe { (*ci).entry.tqe_next = std::ptr::null_mut::<screen_write_citem>() });
        (unsafe { (*ci).entry.tqe_prev = (*cl).items.tqh_last });
        (unsafe { *(*cl).items.tqh_last = ci });
        (unsafe { (*cl).items.tqh_last = &mut (*ci).entry.tqe_next });
    } else {
        (unsafe { (*ci).entry.tqe_prev = (*before).entry.tqe_prev });
        (unsafe { (*ci).entry.tqe_next = before });
        (unsafe { *(*before).entry.tqe_prev = ci });
        (unsafe { (*before).entry.tqe_prev = &mut (*ci).entry.tqe_next });
    }
    (unsafe { (*ctx).item = screen_write_get_citem() });
    (unsafe {
        log_debug(
            b"%s: %u %.*s (at %u,%u)\0" as *const u8 as *const i8,
            (*::core::mem::transmute::<&[u8; 25], &[i8; 25]>(b"screen_write_collect_end\0"))
                .as_ptr(),
            (*ci).used,
            (*ci).used as i32,
            ((*cl).data).offset((*ci).x as isize),
            (*s).cx,
            (*s).cy,
        )
    });
    if (unsafe { (*s).cx }) != 0 as i32 as u32 {
        xx = unsafe { (*s).cx };
        while xx > 0 as i32 as u32 {
            (unsafe { grid_view_get_cell((*s).grid, xx, (*s).cy, &mut gc) });
            if !(gc.flags as i32) & 0x4 as i32 != 0 {
                break;
            }
            (unsafe { grid_view_set_cell((*s).grid, xx, (*s).cy, &grid_default_cell) });
            xx = xx.wrapping_sub(1);
        }
        if gc.data.width as i32 > 1 as i32 {
            (unsafe { grid_view_set_cell((*s).grid, xx, (*s).cy, &grid_default_cell) });
        }
    }
    (unsafe {
        grid_view_set_cells(
            (*s).grid,
            (*s).cx,
            (*s).cy,
            &mut (*ci).gc,
            ((*cl).data).offset((*ci).x as isize),
            (*ci).used as size_t,
        )
    });
    screen_write_set_cursor(
        ctx,
        (unsafe { (*s).cx }).wrapping_add(unsafe { (*ci).used }) as i32,
        -(1 as i32),
    );
    xx = unsafe { (*s).cx };
    while xx < (unsafe { (*(*s).grid).sx }) {
        (unsafe { grid_view_get_cell((*s).grid, xx, (*s).cy, &mut gc) });
        if !(gc.flags as i32) & 0x4 as i32 != 0 {
            break;
        }
        (unsafe { grid_view_set_cell((*s).grid, xx, (*s).cy, &grid_default_cell) });
        xx = xx.wrapping_add(1);
    }
}
#[no_mangle]
pub extern "C" fn screen_write_collect_add(
    mut ctx: *mut screen_write_ctx,
    mut gc: *const grid_cell,
) {
    let mut s: *mut screen = unsafe { (*ctx).s };
    let mut ci: *mut screen_write_citem = std::ptr::null_mut::<screen_write_citem>();
    let mut sx: u_int = unsafe { (*(*s).grid).sx };
    let mut collect: i32 = 0;
    collect = 1 as i32;
    if (unsafe { (*gc).data.width }) as i32 != 1 as i32
        || (unsafe { (*gc).data.size }) as i32 != 1 as i32
        || (unsafe { *((*gc).data.data).as_ptr() }) as i32 >= 0x7f as i32
    {
        collect = 0 as i32;
    } else if (unsafe { (*gc).flags }) as i32 & 0x80 as i32 != 0 {
        collect = 0 as i32;
    } else if (unsafe { (*gc).attr }) as i32 & 0x80 as i32 != 0 {
        collect = 0 as i32;
    } else if !(unsafe { (*s).mode }) & 0x10 as i32 != 0 {
        collect = 0 as i32;
    } else if (unsafe { (*s).mode }) & 0x2 as i32 != 0 {
        collect = 0 as i32;
    } else if !(unsafe { (*s).sel }).is_null() {
        collect = 0 as i32;
    }
    if collect == 0 {
        screen_write_collect_end(ctx);
        screen_write_collect_flush(
            ctx,
            0 as i32,
            (*(unsafe {
                ::core::mem::transmute::<&[u8; 25], &[i8; 25]>(b"screen_write_collect_add\0")
            }))
            .as_ptr(),
        );
        screen_write_cell(ctx, gc);
        return;
    }
    if (unsafe { (*s).cx }) > sx.wrapping_sub(1 as i32 as u32)
        || (unsafe { (*(*ctx).item).used })
            > sx.wrapping_sub(1 as i32 as u32)
                .wrapping_sub(unsafe { (*s).cx })
    {
        screen_write_collect_end(ctx);
    }
    ci = unsafe { (*ctx).item };
    if (unsafe { (*s).cx }) > sx.wrapping_sub(1 as i32 as u32) {
        (unsafe {
            log_debug(
                b"%s: wrapped at %u,%u\0" as *const u8 as *const i8,
                (*::core::mem::transmute::<&[u8; 25], &[i8; 25]>(b"screen_write_collect_add\0"))
                    .as_ptr(),
                (*s).cx,
                (*s).cy,
            )
        });
        (unsafe { (*ci).wrapped = 1 as i32 });
        screen_write_linefeed(ctx, 1 as i32, 8 as i32 as u_int);
        screen_write_set_cursor(ctx, 0 as i32, -(1 as i32));
    }
    if (unsafe { (*ci).used }) == 0 as i32 as u32 {
        (unsafe {
            memcpy(
                &mut (*ci).gc as *mut grid_cell as *mut libc::c_void,
                gc as *const libc::c_void,
                ::core::mem::size_of::<grid_cell>() as u64,
            )
        });
    }
    if (unsafe { (*((*(*ctx).s).write_list).offset((*s).cy as isize)).data }).is_null() {
        let fresh14 = unsafe { &mut (*((*(*ctx).s).write_list).offset((*s).cy as isize)).data };
        *fresh14 = (unsafe { xmalloc((*(*(*ctx).s).grid).sx as size_t) }) as *mut i8;
    }
    let fresh15 = unsafe { (*ci).used };
    (unsafe { (*ci).used = ((*ci).used).wrapping_add(1) });
    (unsafe {
        *((*((*(*ctx).s).write_list).offset((*s).cy as isize)).data)
            .offset(((*s).cx).wrapping_add(fresh15) as isize) =
            (*gc).data.data[0 as i32 as usize] as i8
    });
}
#[no_mangle]
pub extern "C" fn screen_write_cell(mut ctx: *mut screen_write_ctx, mut gc: *const grid_cell) {
    let mut s: *mut screen = unsafe { (*ctx).s };
    let mut gd: *mut grid = unsafe { (*s).grid };
    let mut ud: *const utf8_data = unsafe { &(*gc).data };
    let mut gl: *mut grid_line = std::ptr::null_mut::<grid_line>();
    let mut gce: *mut grid_cell_entry = std::ptr::null_mut::<grid_cell_entry>();
    let mut tmp_gc: grid_cell = grid_cell {
        data: utf8_data {
            data: [0; 21],
            have: 0,
            size: 0,
            width: 0,
        },
        attr: 0,
        flags: 0,
        fg: 0,
        bg: 0,
        us: 0,
        link: 0,
    };
    let mut now_gc: grid_cell = grid_cell {
        data: utf8_data {
            data: [0; 21],
            have: 0,
            size: 0,
            width: 0,
        },
        attr: 0,
        flags: 0,
        fg: 0,
        bg: 0,
        us: 0,
        link: 0,
    };
    let mut ttyctx: tty_ctx = tty_ctx {
        s: std::ptr::null_mut::<screen>(),
        redraw_cb: None,
        set_client_cb: None,
        arg: std::ptr::null_mut::<libc::c_void>(),
        cell: std::ptr::null::<grid_cell>(),
        wrapped: 0,
        num: 0,
        ptr: std::ptr::null_mut::<libc::c_void>(),
        ptr2: std::ptr::null_mut::<libc::c_void>(),
        allow_invisible_panes: 0,
        ocx: 0,
        ocy: 0,
        orupper: 0,
        orlower: 0,
        xoff: 0,
        yoff: 0,
        rxoff: 0,
        ryoff: 0,
        sx: 0,
        sy: 0,
        bg: 0,
        defaults: grid_cell {
            data: utf8_data {
                data: [0; 21],
                have: 0,
                size: 0,
                width: 0,
            },
            attr: 0,
            flags: 0,
            fg: 0,
            bg: 0,
            us: 0,
            link: 0,
        },
        palette: std::ptr::null_mut::<colour_palette>(),
        bigger: 0,
        wox: 0,
        woy: 0,
        wsx: 0,
        wsy: 0,
    };
    let mut sx: u_int = unsafe { (*(*s).grid).sx };
    let mut sy: u_int = unsafe { (*(*s).grid).sy };
    let mut width: u_int = (unsafe { (*ud).width }) as u_int;
    let mut xx: u_int = 0;
    let mut not_wrap: u_int = 0;
    let mut selected: i32 = 0;
    let mut skip: i32 = 1 as i32;
    if (unsafe { (*gc).flags }) as i32 & 0x4 as i32 != 0 {
        return;
    }
    if screen_write_combine(ctx, gc) != 0 as i32 {
        return;
    }
    screen_write_collect_flush(
        ctx,
        1 as i32,
        (*(unsafe { ::core::mem::transmute::<&[u8; 18], &[i8; 18]>(b"screen_write_cell\0") }))
            .as_ptr(),
    );
    if !(unsafe { (*s).mode }) & 0x10 as i32 != 0
        && width > 1 as i32 as u32
        && (width > sx
            || (unsafe { (*s).cx }) != sx && (unsafe { (*s).cx }) > sx.wrapping_sub(width))
    {
        return;
    }
    if (unsafe { (*s).mode }) & 0x2 as i32 != 0 {
        (unsafe { grid_view_insert_cells((*s).grid, (*s).cx, (*s).cy, width, 8 as i32 as u_int) });
        skip = 0 as i32;
    }
    if (unsafe { (*s).mode }) & 0x10 as i32 != 0 && (unsafe { (*s).cx }) > sx.wrapping_sub(width) {
        (unsafe {
            log_debug(
                b"%s: wrapped at %u,%u\0" as *const u8 as *const i8,
                (*::core::mem::transmute::<&[u8; 18], &[i8; 18]>(b"screen_write_cell\0")).as_ptr(),
                (*s).cx,
                (*s).cy,
            )
        });
        screen_write_linefeed(ctx, 1 as i32, 8 as i32 as u_int);
        screen_write_set_cursor(ctx, 0 as i32, -(1 as i32));
        screen_write_collect_flush(
            ctx,
            1 as i32,
            (*(unsafe { ::core::mem::transmute::<&[u8; 18], &[i8; 18]>(b"screen_write_cell\0") }))
                .as_ptr(),
        );
    }
    if (unsafe { (*s).cx }) > sx.wrapping_sub(width)
        || (unsafe { (*s).cy }) > sy.wrapping_sub(1 as i32 as u32)
    {
        return;
    }
    screen_write_initctx(ctx, &mut ttyctx, 0 as i32);
    gl = unsafe { grid_get_line((*s).grid, ((*(*s).grid).hsize).wrapping_add((*s).cy)) };
    if (unsafe { (*gl).flags }) & 0x2 as i32 != 0 {
        (unsafe { grid_view_get_cell(gd, (*s).cx, (*s).cy, &mut now_gc) });
        if screen_write_overwrite(ctx, &mut now_gc, width) != 0 {
            skip = 0 as i32;
        }
    }
    xx = (unsafe { (*s).cx }).wrapping_add(1 as i32 as u32);
    while xx < (unsafe { (*s).cx }).wrapping_add(width) {
        (unsafe {
            log_debug(
                b"%s: new padding at %u,%u\0" as *const u8 as *const i8,
                (*::core::mem::transmute::<&[u8; 18], &[i8; 18]>(b"screen_write_cell\0")).as_ptr(),
                xx,
                (*s).cy,
            )
        });
        (unsafe { grid_view_set_padding(gd, xx, (*s).cy) });
        skip = 0 as i32;
        xx = xx.wrapping_add(1);
    }
    if skip != 0 {
        if (unsafe { (*s).cx }) >= (unsafe { (*gl).cellsize }) {
            skip = unsafe { grid_cells_equal(gc, &grid_default_cell) };
        } else {
            gce = (unsafe { &mut *((*gl).celldata).offset((*s).cx as isize) })
                as *mut grid_cell_entry;
            if (unsafe { (*gce).flags }) as i32 & 0x8 as i32 != 0 {
                skip = 0 as i32;
            } else if (unsafe { (*gc).flags }) as i32 != (unsafe { (*gce).flags }) as i32 {
                skip = 0 as i32;
            } else if (unsafe { (*gc).attr }) as i32
                != (unsafe { (*gce).c2rust_unnamed.data.attr }) as i32
            {
                skip = 0 as i32;
            } else if (unsafe { (*gc).fg }) != (unsafe { (*gce).c2rust_unnamed.data.fg }) as i32 {
                skip = 0 as i32;
            } else if (unsafe { (*gc).bg }) != (unsafe { (*gce).c2rust_unnamed.data.bg }) as i32 {
                skip = 0 as i32;
            } else if (unsafe { (*gc).data.width }) as i32 != 1 as i32 {
                skip = 0 as i32;
            } else if (unsafe { (*gc).data.size }) as i32 != 1 as i32 {
                skip = 0 as i32;
            } else if (unsafe { (*gce).c2rust_unnamed.data.data }) as i32
                != (unsafe { (*gc).data.data[0 as i32 as usize] }) as i32
            {
                skip = 0 as i32;
            }
        }
    }
    selected = unsafe { screen_check_selection(s, (*s).cx, (*s).cy) };
    if selected != 0 && !((unsafe { (*gc).flags }) as i32) & 0x10 as i32 != 0 {
        (unsafe {
            memcpy(
                &mut tmp_gc as *mut grid_cell as *mut libc::c_void,
                gc as *const libc::c_void,
                ::core::mem::size_of::<grid_cell>() as u64,
            )
        });
        tmp_gc.flags = (tmp_gc.flags as i32 | 0x10 as i32) as u_char;
        (unsafe { grid_view_set_cell(gd, (*s).cx, (*s).cy, &mut tmp_gc) });
    } else if selected == 0 && (unsafe { (*gc).flags }) as i32 & 0x10 as i32 != 0 {
        (unsafe {
            memcpy(
                &mut tmp_gc as *mut grid_cell as *mut libc::c_void,
                gc as *const libc::c_void,
                ::core::mem::size_of::<grid_cell>() as u64,
            )
        });
        tmp_gc.flags = (tmp_gc.flags as i32 & !(0x10 as i32)) as u_char;
        (unsafe { grid_view_set_cell(gd, (*s).cx, (*s).cy, &mut tmp_gc) });
    } else if skip == 0 {
        (unsafe { grid_view_set_cell(gd, (*s).cx, (*s).cy, gc) });
    }
    if selected != 0 {
        skip = 0 as i32;
    }
    not_wrap = ((unsafe { (*s).mode }) & 0x10 as i32 == 0) as i32 as u_int;
    if (unsafe { (*s).cx }) <= sx.wrapping_sub(not_wrap).wrapping_sub(width) {
        screen_write_set_cursor(
            ctx,
            (unsafe { (*s).cx }).wrapping_add(width) as i32,
            -(1 as i32),
        );
    } else {
        screen_write_set_cursor(ctx, sx.wrapping_sub(not_wrap) as i32, -(1 as i32));
    }
    if (unsafe { (*s).mode }) & 0x2 as i32 != 0 {
        screen_write_collect_flush(
            ctx,
            0 as i32,
            (*(unsafe { ::core::mem::transmute::<&[u8; 18], &[i8; 18]>(b"screen_write_cell\0") }))
                .as_ptr(),
        );
        ttyctx.num = width;
        (unsafe {
            tty_write(
                Some(
                    tty_cmd_insertcharacter as unsafe extern "C" fn(*mut tty, *const tty_ctx) -> (),
                ),
                &mut ttyctx,
            )
        });
    }
    if skip == 0 {
        if selected != 0 {
            (unsafe { screen_select_cell(s, &mut tmp_gc, gc) });
            ttyctx.cell = &mut tmp_gc;
        } else {
            ttyctx.cell = gc;
        }
        (unsafe {
            tty_write(
                Some(tty_cmd_cell as unsafe extern "C" fn(*mut tty, *const tty_ctx) -> ()),
                &mut ttyctx,
            )
        });
    }
}
extern "C" fn screen_write_combine(
    mut ctx: *mut screen_write_ctx,
    mut gc: *const grid_cell,
) -> i32 {
    let mut s: *mut screen = unsafe { (*ctx).s };
    let mut gd: *mut grid = unsafe { (*s).grid };
    let mut ud: *const utf8_data = unsafe { &(*gc).data };
    let mut n: u_int = 0;
    let mut cx: u_int = unsafe { (*s).cx };
    let mut cy: u_int = unsafe { (*s).cy };
    let mut last: grid_cell = grid_cell {
        data: utf8_data {
            data: [0; 21],
            have: 0,
            size: 0,
            width: 0,
        },
        attr: 0,
        flags: 0,
        fg: 0,
        bg: 0,
        us: 0,
        link: 0,
    };
    let mut ttyctx: tty_ctx = tty_ctx {
        s: std::ptr::null_mut::<screen>(),
        redraw_cb: None,
        set_client_cb: None,
        arg: std::ptr::null_mut::<libc::c_void>(),
        cell: std::ptr::null::<grid_cell>(),
        wrapped: 0,
        num: 0,
        ptr: std::ptr::null_mut::<libc::c_void>(),
        ptr2: std::ptr::null_mut::<libc::c_void>(),
        allow_invisible_panes: 0,
        ocx: 0,
        ocy: 0,
        orupper: 0,
        orlower: 0,
        xoff: 0,
        yoff: 0,
        rxoff: 0,
        ryoff: 0,
        sx: 0,
        sy: 0,
        bg: 0,
        defaults: grid_cell {
            data: utf8_data {
                data: [0; 21],
                have: 0,
                size: 0,
                width: 0,
            },
            attr: 0,
            flags: 0,
            fg: 0,
            bg: 0,
            us: 0,
            link: 0,
        },
        palette: std::ptr::null_mut::<colour_palette>(),
        bigger: 0,
        wox: 0,
        woy: 0,
        wsx: 0,
        wsy: 0,
    };
    let mut force_wide: i32 = 0 as i32;
    let mut zero_width: i32 = 0 as i32;
    if (unsafe { utf8_is_zwj(ud) }) != 0 {
        zero_width = 1 as i32;
    } else if (unsafe { utf8_is_vs(ud) }) != 0 {
        force_wide = 1 as i32;
        zero_width = force_wide;
    } else if (unsafe { (*ud).width }) as i32 == 0 as i32 {
        zero_width = 1 as i32;
    }
    if ((unsafe { (*ud).size }) as i32) < 2 as i32 || cx == 0 as i32 as u32 {
        return zero_width;
    }
    (unsafe {
        log_debug(
            b"%s: character %.*s at %u,%u (width %u)\0" as *const u8 as *const i8,
            (*::core::mem::transmute::<&[u8; 21], &[i8; 21]>(b"screen_write_combine\0")).as_ptr(),
            (*ud).size as i32,
            ((*ud).data).as_ptr(),
            cx,
            cy,
            (*ud).width as i32,
        )
    });
    n = 1 as i32 as u_int;
    (unsafe { grid_view_get_cell(gd, cx.wrapping_sub(n), cy, &mut last) });
    if cx != 1 as i32 as u32 && last.flags as i32 & 0x4 as i32 != 0 {
        n = 2 as i32 as u_int;
        (unsafe { grid_view_get_cell(gd, cx.wrapping_sub(n), cy, &mut last) });
    }
    if n != last.data.width as u32 || last.flags as i32 & 0x4 as i32 != 0 {
        return zero_width;
    }
    if zero_width == 0 {
        if (unsafe { utf8_is_modifier(ud) }) != 0 {
            if (last.data.size as i32) < 2 as i32 {
                return 0 as i32;
            }
            force_wide = 1 as i32;
        } else if (unsafe { utf8_has_zwj(&mut last.data) }) == 0 {
            return 0 as i32;
        }
    }
    if (last.data.size as i32 + (unsafe { (*ud).size }) as i32) as u64
        > ::core::mem::size_of::<[u_char; 21]>() as u64
    {
        return 0 as i32;
    }
    screen_write_collect_flush(
        ctx,
        0 as i32,
        (*(unsafe { ::core::mem::transmute::<&[u8; 21], &[i8; 21]>(b"screen_write_combine\0") }))
            .as_ptr(),
    );
    (unsafe {
        log_debug(
            b"%s: %.*s -> %.*s at %u,%u (offset %u, width %u)\0" as *const u8 as *const i8,
            (*::core::mem::transmute::<&[u8; 21], &[i8; 21]>(b"screen_write_combine\0")).as_ptr(),
            (*ud).size as i32,
            ((*ud).data).as_ptr(),
            last.data.size as i32,
            (last.data.data).as_mut_ptr(),
            cx.wrapping_sub(n),
            cy,
            n,
            last.data.width as i32,
        )
    });
    (unsafe {
        memcpy(
            (last.data.data)
                .as_mut_ptr()
                .offset(last.data.size as i32 as isize) as *mut libc::c_void,
            ((*ud).data).as_ptr() as *const libc::c_void,
            (*ud).size as u64,
        )
    });
    last.data.size = (last.data.size as i32 + (unsafe { (*ud).size }) as i32) as u_char;
    if last.data.width as i32 == 1 as i32 && force_wide != 0 {
        last.data.width = 2 as i32 as u_char;
        n = 2 as i32 as u_int;
        cx = cx.wrapping_add(1);
    } else {
        force_wide = 0 as i32;
    }
    (unsafe { grid_view_set_cell(gd, cx.wrapping_sub(n), cy, &mut last) });
    if force_wide != 0 {
        (unsafe { grid_view_set_padding(gd, cx.wrapping_sub(1 as i32 as u32), cy) });
    }
    screen_write_set_cursor(ctx, cx.wrapping_sub(n) as i32, cy as i32);
    screen_write_initctx(ctx, &mut ttyctx, 0 as i32);
    ttyctx.cell = &mut last;
    ttyctx.num = force_wide as u_int;
    (unsafe {
        tty_write(
            Some(tty_cmd_cell as unsafe extern "C" fn(*mut tty, *const tty_ctx) -> ()),
            &mut ttyctx,
        )
    });
    screen_write_set_cursor(ctx, cx as i32, cy as i32);
    return 1 as i32;
}
extern "C" fn screen_write_overwrite(
    mut ctx: *mut screen_write_ctx,
    mut gc: *mut grid_cell,
    mut width: u_int,
) -> i32 {
    let mut s: *mut screen = unsafe { (*ctx).s };
    let mut gd: *mut grid = unsafe { (*s).grid };
    let mut tmp_gc: grid_cell = grid_cell {
        data: utf8_data {
            data: [0; 21],
            have: 0,
            size: 0,
            width: 0,
        },
        attr: 0,
        flags: 0,
        fg: 0,
        bg: 0,
        us: 0,
        link: 0,
    };
    let mut xx: u_int = 0;
    let mut done: i32 = 0 as i32;
    if (unsafe { (*gc).flags }) as i32 & 0x4 as i32 != 0 {
        xx = (unsafe { (*s).cx }).wrapping_add(1 as i32 as u32);
        loop {
            xx = xx.wrapping_sub(1);
            if xx <= 0 as i32 as u32 {
                break;
            }
            (unsafe { grid_view_get_cell(gd, xx, (*s).cy, &mut tmp_gc) });
            if !(tmp_gc.flags as i32) & 0x4 as i32 != 0 {
                break;
            }
            (unsafe {
                log_debug(
                    b"%s: padding at %u,%u\0" as *const u8 as *const i8,
                    (*::core::mem::transmute::<&[u8; 23], &[i8; 23]>(b"screen_write_overwrite\0"))
                        .as_ptr(),
                    xx,
                    (*s).cy,
                )
            });
            (unsafe { grid_view_set_cell(gd, xx, (*s).cy, &grid_default_cell) });
        }
        (unsafe {
            log_debug(
                b"%s: character at %u,%u\0" as *const u8 as *const i8,
                (*::core::mem::transmute::<&[u8; 23], &[i8; 23]>(b"screen_write_overwrite\0"))
                    .as_ptr(),
                xx,
                (*s).cy,
            )
        });
        (unsafe { grid_view_set_cell(gd, xx, (*s).cy, &grid_default_cell) });
        done = 1 as i32;
    }
    if width != 1 as i32 as u32
        || (unsafe { (*gc).data.width }) as i32 != 1 as i32
        || (unsafe { (*gc).flags }) as i32 & 0x4 as i32 != 0
    {
        xx = (unsafe { (*s).cx })
            .wrapping_add(width)
            .wrapping_sub(1 as i32 as u32);
        loop {
            xx = xx.wrapping_add(1);
            if xx >= (unsafe { (*(*s).grid).sx }) {
                break;
            }
            (unsafe { grid_view_get_cell(gd, xx, (*s).cy, &mut tmp_gc) });
            if !(tmp_gc.flags as i32) & 0x4 as i32 != 0 {
                break;
            }
            (unsafe {
                log_debug(
                    b"%s: overwrite at %u,%u\0" as *const u8 as *const i8,
                    (*::core::mem::transmute::<&[u8; 23], &[i8; 23]>(b"screen_write_overwrite\0"))
                        .as_ptr(),
                    xx,
                    (*s).cy,
                )
            });
            if (unsafe { (*gc).flags }) as i32 & 0x80 as i32 != 0 {
                (unsafe {
                    memcpy(
                        &mut tmp_gc as *mut grid_cell as *mut libc::c_void,
                        gc as *const libc::c_void,
                        ::core::mem::size_of::<grid_cell>() as u64,
                    )
                });
                (unsafe {
                    memset(
                        (tmp_gc.data.data).as_mut_ptr() as *mut libc::c_void,
                        0 as i32,
                        ::core::mem::size_of::<[u_char; 21]>() as u64,
                    )
                });
                (unsafe { *(tmp_gc.data.data).as_mut_ptr() = ' ' as i32 as u_char });
                tmp_gc.data.have = 1 as i32 as u_char;
                tmp_gc.data.size = tmp_gc.data.have;
                tmp_gc.data.width = tmp_gc.data.size;
                (unsafe { grid_view_set_cell(gd, xx, (*s).cy, &mut tmp_gc) });
            } else {
                (unsafe { grid_view_set_cell(gd, xx, (*s).cy, &grid_default_cell) });
            }
            done = 1 as i32;
        }
    }
    return done;
}
#[no_mangle]
pub extern "C" fn screen_write_setselection(
    mut ctx: *mut screen_write_ctx,
    mut flags: *const i8,
    mut str: *mut u_char,
    mut len: u_int,
) {
    let mut ttyctx: tty_ctx = tty_ctx {
        s: std::ptr::null_mut::<screen>(),
        redraw_cb: None,
        set_client_cb: None,
        arg: std::ptr::null_mut::<libc::c_void>(),
        cell: std::ptr::null::<grid_cell>(),
        wrapped: 0,
        num: 0,
        ptr: std::ptr::null_mut::<libc::c_void>(),
        ptr2: std::ptr::null_mut::<libc::c_void>(),
        allow_invisible_panes: 0,
        ocx: 0,
        ocy: 0,
        orupper: 0,
        orlower: 0,
        xoff: 0,
        yoff: 0,
        rxoff: 0,
        ryoff: 0,
        sx: 0,
        sy: 0,
        bg: 0,
        defaults: grid_cell {
            data: utf8_data {
                data: [0; 21],
                have: 0,
                size: 0,
                width: 0,
            },
            attr: 0,
            flags: 0,
            fg: 0,
            bg: 0,
            us: 0,
            link: 0,
        },
        palette: std::ptr::null_mut::<colour_palette>(),
        bigger: 0,
        wox: 0,
        woy: 0,
        wsx: 0,
        wsy: 0,
    };
    screen_write_initctx(ctx, &mut ttyctx, 0 as i32);
    ttyctx.ptr = str as *mut libc::c_void;
    ttyctx.ptr2 = flags as *mut libc::c_void;
    ttyctx.num = len;
    (unsafe {
        tty_write(
            Some(tty_cmd_setselection as unsafe extern "C" fn(*mut tty, *const tty_ctx) -> ()),
            &mut ttyctx,
        )
    });
}
#[no_mangle]
pub extern "C" fn screen_write_rawstring(
    mut ctx: *mut screen_write_ctx,
    mut str: *mut u_char,
    mut len: u_int,
    mut allow_invisible_panes: i32,
) {
    let mut ttyctx: tty_ctx = tty_ctx {
        s: std::ptr::null_mut::<screen>(),
        redraw_cb: None,
        set_client_cb: None,
        arg: std::ptr::null_mut::<libc::c_void>(),
        cell: std::ptr::null::<grid_cell>(),
        wrapped: 0,
        num: 0,
        ptr: std::ptr::null_mut::<libc::c_void>(),
        ptr2: std::ptr::null_mut::<libc::c_void>(),
        allow_invisible_panes: 0,
        ocx: 0,
        ocy: 0,
        orupper: 0,
        orlower: 0,
        xoff: 0,
        yoff: 0,
        rxoff: 0,
        ryoff: 0,
        sx: 0,
        sy: 0,
        bg: 0,
        defaults: grid_cell {
            data: utf8_data {
                data: [0; 21],
                have: 0,
                size: 0,
                width: 0,
            },
            attr: 0,
            flags: 0,
            fg: 0,
            bg: 0,
            us: 0,
            link: 0,
        },
        palette: std::ptr::null_mut::<colour_palette>(),
        bigger: 0,
        wox: 0,
        woy: 0,
        wsx: 0,
        wsy: 0,
    };
    screen_write_initctx(ctx, &mut ttyctx, 0 as i32);
    ttyctx.ptr = str as *mut libc::c_void;
    ttyctx.num = len;
    ttyctx.allow_invisible_panes = allow_invisible_panes;
    (unsafe {
        tty_write(
            Some(tty_cmd_rawstring as unsafe extern "C" fn(*mut tty, *const tty_ctx) -> ()),
            &mut ttyctx,
        )
    });
}
#[no_mangle]
pub extern "C" fn screen_write_alternateon(
    mut ctx: *mut screen_write_ctx,
    mut gc: *mut grid_cell,
    mut cursor: i32,
) {
    let mut ttyctx: tty_ctx = tty_ctx {
        s: std::ptr::null_mut::<screen>(),
        redraw_cb: None,
        set_client_cb: None,
        arg: std::ptr::null_mut::<libc::c_void>(),
        cell: std::ptr::null::<grid_cell>(),
        wrapped: 0,
        num: 0,
        ptr: std::ptr::null_mut::<libc::c_void>(),
        ptr2: std::ptr::null_mut::<libc::c_void>(),
        allow_invisible_panes: 0,
        ocx: 0,
        ocy: 0,
        orupper: 0,
        orlower: 0,
        xoff: 0,
        yoff: 0,
        rxoff: 0,
        ryoff: 0,
        sx: 0,
        sy: 0,
        bg: 0,
        defaults: grid_cell {
            data: utf8_data {
                data: [0; 21],
                have: 0,
                size: 0,
                width: 0,
            },
            attr: 0,
            flags: 0,
            fg: 0,
            bg: 0,
            us: 0,
            link: 0,
        },
        palette: std::ptr::null_mut::<colour_palette>(),
        bigger: 0,
        wox: 0,
        woy: 0,
        wsx: 0,
        wsy: 0,
    };
    let mut wp: *mut window_pane = unsafe { (*ctx).wp };
    if !wp.is_null()
        && (unsafe {
            options_get_number(
                (*wp).options,
                b"alternate-screen\0" as *const u8 as *const i8,
            )
        }) == 0
    {
        return;
    }
    screen_write_collect_flush(
        ctx,
        0 as i32,
        (*(unsafe {
            ::core::mem::transmute::<&[u8; 25], &[i8; 25]>(b"screen_write_alternateon\0")
        }))
        .as_ptr(),
    );
    (unsafe { screen_alternate_on((*ctx).s, gc, cursor) });
    if !wp.is_null() {
        (unsafe { layout_fix_panes((*wp).window, std::ptr::null_mut::<window_pane>()) });
    }
    screen_write_initctx(ctx, &mut ttyctx, 1 as i32);
    if (ttyctx.redraw_cb).is_some() {
        (unsafe { (ttyctx.redraw_cb).expect("non-null function pointer")(&mut ttyctx) });
    }
}
#[no_mangle]
pub extern "C" fn screen_write_alternateoff(
    mut ctx: *mut screen_write_ctx,
    mut gc: *mut grid_cell,
    mut cursor: i32,
) {
    let mut ttyctx: tty_ctx = tty_ctx {
        s: std::ptr::null_mut::<screen>(),
        redraw_cb: None,
        set_client_cb: None,
        arg: std::ptr::null_mut::<libc::c_void>(),
        cell: std::ptr::null::<grid_cell>(),
        wrapped: 0,
        num: 0,
        ptr: std::ptr::null_mut::<libc::c_void>(),
        ptr2: std::ptr::null_mut::<libc::c_void>(),
        allow_invisible_panes: 0,
        ocx: 0,
        ocy: 0,
        orupper: 0,
        orlower: 0,
        xoff: 0,
        yoff: 0,
        rxoff: 0,
        ryoff: 0,
        sx: 0,
        sy: 0,
        bg: 0,
        defaults: grid_cell {
            data: utf8_data {
                data: [0; 21],
                have: 0,
                size: 0,
                width: 0,
            },
            attr: 0,
            flags: 0,
            fg: 0,
            bg: 0,
            us: 0,
            link: 0,
        },
        palette: std::ptr::null_mut::<colour_palette>(),
        bigger: 0,
        wox: 0,
        woy: 0,
        wsx: 0,
        wsy: 0,
    };
    let mut wp: *mut window_pane = unsafe { (*ctx).wp };
    if !wp.is_null()
        && (unsafe {
            options_get_number(
                (*wp).options,
                b"alternate-screen\0" as *const u8 as *const i8,
            )
        }) == 0
    {
        return;
    }
    screen_write_collect_flush(
        ctx,
        0 as i32,
        (*(unsafe {
            ::core::mem::transmute::<&[u8; 26], &[i8; 26]>(b"screen_write_alternateoff\0")
        }))
        .as_ptr(),
    );
    (unsafe { screen_alternate_off((*ctx).s, gc, cursor) });
    if !wp.is_null() {
        (unsafe { layout_fix_panes((*wp).window, std::ptr::null_mut::<window_pane>()) });
    }
    screen_write_initctx(ctx, &mut ttyctx, 1 as i32);
    if (ttyctx.redraw_cb).is_some() {
        (unsafe { (ttyctx.redraw_cb).expect("non-null function pointer")(&mut ttyctx) });
    }
}
extern "C" fn run_static_initializers() {
    (unsafe {
        screen_write_citem_freelist = {
            let mut init = C2RustUnnamed_37 {
                tqh_first: std::ptr::null_mut::<screen_write_citem>(),
                tqh_last: &mut screen_write_citem_freelist.tqh_first,
            };
            init
        }
    });
}
#[used]
#[cfg_attr(target_os = "linux", link_section = ".init_array")]
#[cfg_attr(target_os = "windows", link_section = ".CRT$XIB")]
#[cfg_attr(target_os = "macos", link_section = "__DATA,__mod_init_func")]
static INIT_ARRAY: [unsafe extern "C" fn(); 1] = [run_static_initializers];
